Loggers#

A logger is the main entry point of the logging library, allowing you to define top-level behavior.

import logging

List all loggers#

It’s usefull to acesss all loggers available now. All loogets defined in the cureent interprier state is defined in the logging.Logger.manager.loggerDict.


The following cell demonstrates how loggerDict looks like for the current jupyter notebook.

logging.Logger.manager.loggerDict
{'concurrent.futures': <Logger concurrent.futures (WARNING)>,
 'concurrent': <logging.PlaceHolder at 0x77013bedf040>,
 'asyncio': <Logger asyncio (WARNING)>,
 'tornado.access': <Logger tornado.access (WARNING)>,
 'tornado': <Logger tornado (WARNING)>,
 'tornado.application': <Logger tornado.application (WARNING)>,
 'tornado.general': <Logger tornado.general (WARNING)>,
 'stack_data.serializing': <Logger stack_data.serializing (WARNING)>,
 'stack_data': <logging.PlaceHolder at 0x77013ad56fe0>,
 'parso': <Logger parso (WARNING)>,
 'prompt_toolkit.buffer': <Logger prompt_toolkit.buffer (WARNING)>,
 'prompt_toolkit': <logging.PlaceHolder at 0x77013a970d60>,
 'parso.python.diff': <Logger parso.python.diff (WARNING)>,
 'parso.python': <logging.PlaceHolder at 0x77013a3eb040>,
 'parso.cache': <Logger parso.cache (WARNING)>,
 'Comm': <Logger Comm (WARNING)>,
 'ipykernel.comm': <Logger ipykernel.comm (WARNING)>,
 'ipykernel': <logging.PlaceHolder at 0x770139edcaf0>,
 'IPKernelApp': <Logger IPKernelApp (DEBUG)>}

Disable#

Some loggers can be disabled. A logger is disabled if it’s disabled property takes the value false.


The following example shows how created logger can be disabled.

logger = logging.getLogger("disable_test_logger")
logger.warning("beep")
logger.disabled = True
logger.warning("beep")
logger.disabled = False
beep

Even though we run the looger.warning("beep") command twice, there is only one beep message because we just set disabled=True before the second beep.

Logger hierarchy#

There is a hierarchy of loggers. This hierarchy is defined simply by the names of the loggers, in the format <parent name>.<child name>. This enables the propagation of the parent logger properties from the parent to the child - you can control the group of loggers configuring the common parent for all of them.


The following cell defines the parent_logger and sets its level to the ERROR.

parent_logger = logging.getLogger("parent_logger")
parent_logger.setLevel(logging.ERROR)
parent_logger.getEffectiveLevel()
40

The following cell creates the child_logger. Its relationship with the parent_logger is defined solely by logger’s full name.

child_logger = logging.getLogger("parent_logger.child_logger")
child_logger.getEffectiveLevel()
40

The child logger follows its settings and does not produce any warning messages:

child_logger.warning("This is a warning from the child logger.")

The following cell sets the parent logger’s level to INFO. And shows the effective levels of both the parent and child loggers:

parent_logger.setLevel(logging.INFO)
(
    parent_logger.getEffectiveLevel(),
    child_logger.getEffectiveLevel()
)
(20, 20)

Consequently, the child logger follows the configuration of the parent logger. The following cell show that the child logger changes behaviour just in a corresponding way:

child_logger.warning("This is a warning from the child logger.")
This is a warning from the child logger.