Is there a way in Python to get a list of all defined loggers?
I mean, does something exist such as logging.getAllLoggers() which would return a list of Logger objects?
I searched the python.logging documentation but couldn't find such a method.
Is there a way in Python to get a list of all defined loggers?
I mean, does something exist such as logging.getAllLoggers() which would return a list of Logger objects?
I searched the python.logging documentation but couldn't find such a method.
Loggers are held in a hierarchy by a logging.Manager instance. You can interrogate the manager on the root logger for the loggers it knows about.
import logging
loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
Calling getLogger(name) ensures that any placeholder loggers held by loggerDict are fully initialized when they are added to the list.
logging.root.manager.loggerDict was exactly what I was searching for, thank you!Unresolved attribute reference 'manager' for class 'RootLogger'? I know it can be easily disabled, but still...pylint doesn't seem to found it. I get a no-member reported for loggerDict.If you want to include RootLogger in the list as well, do something similar to:
import logging
loggers = [logging.getLogger()] # get the root logger
loggers = loggers + [logging.getLogger(name) for name in logging.root.manager.loggerDict]
tested on Python 3.7.4
If you are trying to examine the hierarchy of logging objects, I'd recommend using logging_tree.printout():
import logging_tree
logging_tree.printout()
Or, if you want to have the logging tree accessible to your code:
logging_tree.tree()
See https://pypi.org/project/logging_tree/ for more info.