Beware of disable_existing_loggers when setting up Python logging

There was a warning in the official document Logging environment settings, but I was completely addicted to it, so I wrote an article. I will leave it.

In addition to configuring by calling methods such as addHandler (), Python's logging module is configured based on logging.config.fileConfig (), which is configured based on the ini format configuration file, and dict type configuration information. It can be configured using logging.config.dictConfig ().

import logging.config

logging.config.dictConfig({
    'version': 1,
    'handlers': {
        'default': {
            'class': 'logging.StreamHandler',
            'level': 'DEBUG',
            'stream': 'ext://sys.stderr',
            }
    },
    'root': {
        'level': 'DEBUG',
        'handlers': ['default'],
    }
})

logger = logging.getLogger('example')
logger.info('Hello')  #Output

When using Python logging, the idiom is to write logger = logging.getLogger (__ name__) at the beginning of the module. However, the logging setup I mentioned earlier breaks that code.

import logging.config

logger = logging.getLogger('example')

logging.config.dictConfig({
    'version': 1,
    'handlers': {
        'default': {
            'class': 'logging.StreamHandler',
            'level': 'DEBUG',
            'stream': 'ext://sys.stderr',
            }
    },
    'root': {
        'level': 'DEBUG',
        'handlers': ['default'],
    }
})

logger.info('Hello')  #Not output

This is because fileConfig and dictConfig were originally intended to configure logging as a whole, and by default disable the already generated logger.

The option to customize this behavior is disable_existing_loggers, where True means the default behavior and False means that it will not be disabled. This option can be specified as a keyword argument for fileConfig or as an element of the dict to pass for dictConfig.

import logging.config

logger = logging.getLogger('example')

logging.config.dictConfig({
    'version': 1,
    'handlers': {
        'default': {
            'class': 'logging.StreamHandler',
            'level': 'DEBUG',
            'stream': 'ext://sys.stderr',
            }
    },
    'root': {
        'level': 'DEBUG',
        'handlers': ['default'],
    },
    'disable_existing_loggers': False,
})

logger.info('Hello')  #Output

dictConfig also has an option called incremental, which just adds the handlers and filters that come with the existing logger, without removing them.

Recommended Posts

Beware of disable_existing_loggers when setting up Python logging
Python --Quick start of logging
Setting up Basic authentication using Python @Lambda
A memo when setting up a Docker container for using JUMAN ++, KNP, python
Python Logging
Setting up Digest authentication using Python @Lambda
Character encoding when using csv module of python 2.7.3
[Python] A rough understanding of the logging module
Addition of fixed processing when starting python interpreter
Introduction of Python
[Note] The story of setting up the SDK for Python of Azure IoT Hub on Linux
Basics of Python ①
Basics of python ①
Copy of python
Introduction of Python
From setting up Raspberry Pi to installing Python environment
A memorandum when writing experimental code ~ Logging in python
Note when putting lxml of python package in ubuntu 14.04
Summary of Pandas methods used when extracting data [Python]
Review of atcoder ABC158, up to question E (Python)
Setting up Jupyter Lab in a Python 3.9 venv environment
Memo of "Cython-Speeding up Python by fusing with C"