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

3

59

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?

Philadelphia answered 23/7, 2015 at 23:46 Comment(3)
pypi.python.org/pypi/logging_treeOrtega
Are you asking how to get the current level for a particular logger?Maryannmaryanna
@SimeonVisser, thanks, logging_tree is awesome, can you please post your comment as an answer?Calysta
S
27

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
Shoal answered 30/10, 2018 at 5:55 Comment(0)
L
9

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'>}
Lucilelucilia answered 5/7, 2020 at 14:54 Comment(2)
This is great, as it does not introduce additional (dev) dependencies. The logging_tree package, mentioned in the other answers, uses this under the hood.Elwin
Also see this answer: stackoverflow.com/a/53250066Elwin
M
4

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'
Maryannmaryanna answered 24/7, 2015 at 1:24 Comment(1)
Thank you, Anand. My question was more about how to print the config of all loggers. Simeon answered it, but I appreciate your suggestion.Philadelphia

© 2022 - 2024 — McMap. All rights reserved.