Search code examples
pythonloggingfilterconfigurationhandler

Python logging.conf reload configuration


in my python code I read the logging configuration from a file where I have put the format I would like to have in my code.
When I try to reload the configuration at runtime it seems that the set of some extra env I put in my formatters are lost and I have the following error.

Traceback (most recent call last):
  File "/usr/lib64/python2.7/logging/__init__.py", line 861, in emit
    msg = self.format(record)
  File "/usr/lib64/python2.7/logging/__init__.py", line 734, in format
    return fmt.format(record)
  File "/usr/lib64/python2.7/logging/__init__.py", line 469, in format
    s = self._fmt % record.__dict__
KeyError: 'hostname'
Logged from file conn.py, line 304

To notice that my logging.conf is the following:

[formatter_simpleFormatter]
format=[%(levelname)s] %(asctime)s [ThId-%(threadName)-10s] [%(filename)s:%(funcName)s:%(lineno)s] [%(hostname)s] %(message)s
datefmt=%m/%d/%Y %I:%M:%S %p

where hostname is set in the code via:

class HostnameFilter(logging.Filter):
    hostname = platform.node()

    def filter(self, record):
        record.hostname = HostnameFilter.hostname
        return True

## Log Logger.
class Logger:
    """!@brief
    Logger wrapper
    """
    def __init__(self):
        self.log = logging.getLogger("MYCONF")
        self.log.addFilter(HostnameFilter())

The weird thing is that the errors seems to appear in classes (conn.py) not mine, maybe this is the code of some libraries I am using that are logging as well.

Is this the flow? Shall I do something else?


Solution

  • Because formatting happens in handlers, and events logged to a logger are passed to handlers of that logger and to handlers further up the logger hierarchy, you'd need to be sure that the hostname attribute is always added to the record - which may not be the case if a different logger is used to log the message. Try attaching the filter to the relevant handler(s) rather than logger(s).