Enumerating loggers iin logging module

Tim Chase python.list at tim.thechases.com
Tue Dec 30 16:24:44 EST 2014


On 2014-12-30 18:42, jptechnical.co.uk wrote:
> I've recently started using the logging module and wondered if
> there was a way to enumerate all the Logger objects available as a
> result of calls to "logging.getLogger(name)". Went through the docs
> and could not spot any way of doing this. Have I missed something?
> 
> It would have been useful to list the Loggers created in an app as
> I'd made the mistake of writing:
> in module A:    logging.getLogger("Name")
> in module B:    logging.getLogger("name.sublogger")
> 
> and wondered why messages sent to "name.sublogger" did not get
> passed to the parent handler in "Name". A list of of available
> loggers would have quickly shown the error of my ways.

While it may involve reaching into the objects may or may not be
blessed, the following seems to work for me in Py2.7:

  >>> import logging
  >>> # add a bunch of loggers and sub-loggers
  >>> print logging.Logger.manager.loggerDict.keys()

-tkc






More information about the Python-list mailing list