Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all existing loggers using python.logging module

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.

Thank you in advance.

like image 651
mistiru Avatar asked Nov 11 '18 13:11

mistiru


People also ask

What does logging module do in Python?

Python comes with a logging module in the standard library that provides a flexible framework for emitting log messages from Python programs. This module is widely used by libraries and is the first go-to point for most developers when it comes to logging.

What is logging config in Python?

The logging configuration functionality tries to offer convenience, and in part this is done by offering the ability to convert text in configuration files into Python objects used in logging configuration - for example, as described in User-defined objects.

What are the five levels of logging in Python?

In Python, the built-in logging module can be used to log events. Log messages can have 5 levels - DEBUG, INGO, WARNING, ERROR and CRITICAL. They can also include traceback information for exceptions. Logs can be especially useful in case of errors to help identify their cause.


1 Answers

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.

like image 50
Will Keeling Avatar answered Oct 12 '22 03:10

Will Keeling