How to print current logging configuration used by the python logging module?

Question:

I’m using the python logging module.

I update the logging config using logging.dictConfig().

I would like a way to read the current configuration (e.g. level) used by each logger and print it.

How can I get and print this information?

Asked By: morfys

||

Answers:

If what you want is the logging level for a particular logger, then you can use – logger.getEffectiveLevel() , this would give the integer value for the currently logging level for the logger, and then you can use it with logging.getLevelName() , to get the string representation for that level.

Example –

>>> import logging
>>> l = logging.getLogger(__name__)
>>> l.setLevel(logging.DEBUG)
>>> logging.getLevelName(l.getEffectiveLevel())
'DEBUG'
Answered By: Anand S Kumar

From Simeon’s comment, the logging_tree package lets you print out the details of the current logging configuration.

>>> import logging
>>> logging.getLogger('a')
>>> logging.getLogger('a.b').setLevel(logging.DEBUG)
>>> logging.getLogger('x.c')
>>> from logging_tree import printout
>>> printout()
<--""
   Level WARNING
   |
   o<--"a"
   |   Level NOTSET so inherits level WARNING
   |   |
   |   o<--"a.b"
   |       Level DEBUG
   |
   o<--[x]
       |
       o<--"x.c"
           Level NOTSET so inherits level WARNING
>>> # Get the same display as a string:
>>> from logging_tree.format import build_description
>>> print(build_description()[:50])
<--""
   Level WARNING
   |
   o<--"a"
   |   Leve
Answered By: Don Kirkby

Logging configuration are stored in logging.root.manager.

From there you can access every logging parameters, example : logging.root.manager.root.level to get the logging level of your root logger.

Any other loggers can be accessed through logging.root.manager.loggerDict['logger_name'].

Here is an example on how to get the formatter used by a custom logger:

>>> import logging
>>> import logging.config
>>> config = {"version": 1,
...     "formatters": {
...         "detailed": {
...             "class": "logging.Formatter",
...             "format": "%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s"}},
...     "handlers": {
...         "console": {
...             "class": "logging.StreamHandler",
...             "level": "WARNING"},
...         "file": {
...             "class": "logging.FileHandler",
...             "filename": "mplog.log",
...             "mode": "a",
...             "formatter": "detailed"},
...         "foofile": {
...             "class": "logging.handlers.RotatingFileHandler",
...             "filename": "mplog-foo.log",
...             "mode": "a",
...             "formatter": "detailed",
...             "maxBytes": 20000,
...             "backupCount": 20}},
...     "loggers": {
...         "foo": {
...             "handlers": ["foofile"]}},
...     "root": {
...         "level": "DEBUG",
...         "handlers": ["console", "file"]}}
>>> logging.config.dictConfig(config)
>>> logging.root.manager.loggerDict['foo'].handlers[0].formatter._fmt
'%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'

Handlers parameters can also be found in logging._handlers.data.

>>> logging._handlers.data['file']().__dict__
 {'baseFilename': '/File/Path/mplog.log',
  'mode': 'a',
  'encoding': None,
  'delay': False,
  'filters': [],
  '_name': 'file',
  'level': 0,
  'formatter': <logging.Formatter object at 0x7ff13a3d6df0>,
  'lock': <unlocked _thread.RLock object owner=0 count=0 at 0x7ff13a5b7510>,
  'stream': <_io.TextIOWrapper name='/File/Path/mplog.log' mode='a' encoding='UTF-8'>}
Answered By: Turlututu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.