How to print current logging configuration used by the python logging module?
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
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'>}