Question
Asked By – mistiru
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.
Now we will see solution for issue: How to list all existing loggers using python.logging module
Answer
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.
This question is answered By – Will Keeling
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0