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