I would like to create a Python logging class that can be inherited as a regular way to register configuration, but separately control the level of logging of the base class from the parent. This is similar to How to use python logging in multiple modules . Vinay Sajip's answer for using LogMixin is very close. Below is my modified version.
Most of my classes inherit smaller classes. For instance:
filename: LogMixin.py
import logging, logging.config import yaml class LogMixin(object): __loggerConfigured = False @property def logger(self): if not self.__loggerConfigured: with open('log_config.yaml', 'rt') as f: config = yaml.load(f.read()) logging.config.dictConfig(config) self.__loggerConfigured = True name = '.'.join([self.__class__.__name__]) return logging.getLogger(name)
filename: Base.py
from LogMixin import LogMixin class Base(LogMixin): def __init__(self): self.logger.debug("Debug Base") def run_base(self): self.logger.debug("Debug Running Base") self.logger.info("Info Running Base") if __name__ == '__main__': my_base = Base() my_base.run_base()
filename: Parent.py
from Base import Base class Parent(Base): def __init__(self): self.logger.debug("Debug Parent") def run_parent(self): self.logger.debug("Debug Running Parent") self.logger.info("Info Running Parent") if __name__ == '__main__': my_parent = Parent() my_parent.run_base() my_parent.run_parent()
filename: log_config.yaml
--- version: 1 disable_existing_loggers: False
I get the benefits of a general logging configuration. However, I would like to independently control the log levels for both Base and Parent. With the configuration file above, I get:
$ python Base.py 2015-03-16 00:06:23,716 - Base - INFO - Info Running Base $ python Parent.py 2015-03-16 00:06:19,682 - Parent - DEBUG - Debug Parent 2015-03-16 00:06:19,682 - Parent - DEBUG - Debug Running Base 2015-03-16 00:06:19,682 - Parent - INFO - Info Running Base 2015-03-16 00:06:19,682 - Parent - DEBUG - Debug Running Parent 2015-03-16 00:06:19,682 - Parent - INFO - Info Running Parent
I understand why I understand this; I have only one Parent registrar. However, in general, I would rather get the following:
$ python Base.py 2015-03-16 00:06:23,716 - Base - INFO - Info Running Base $ python Parent.py 2015-03-16 00:06:19,682 - Parent - DEBUG - Debug Parent 2015-03-16 00:06:19,682 - Base - INFO - Info Running Base 2015-03-16 00:06:19,682 - Parent - DEBUG - Debug Running Parent 2015-03-16 00:06:19,682 - Parent - INFO - Info Running Parent
(note the DEBUG associated with Base.py).
Or even better:
$ python Base.py 2015-03-16 00:06:23,716 - Base - INFO - Info Running Base $ python Parent.py 2015-03-16 00:06:19,682 - Parent - DEBUG - Debug Parent 2015-03-16 00:06:19,682 - Parent.Base - INFO - Info Running Base 2015-03-16 00:06:19,682 - Parent - DEBUG - Debug Running Parent 2015-03-16 00:06:19,682 - Parent - INFO - Info Running Parent
(Note that the name is Parent.Base so that I see inheritance.) Is this possible with one simple LogMixin class?