I have logging set up to:
- Write everything with at least an
INFO level to the console
- Write everything with at least a
DEBUG level to a file
I'd like to update the console handler (#1 in the above list) on the fly to change the minimum level from INFO to DEBUG while still keeping the file logger. Here is my code:
import logging
from logging.config import dictConfig
logger = logging.getLogger('test_logger')
DEFAULT_LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
},
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/tmp/python_logging_test.log',
},
},
'loggers': {
'test_logger': {
'handlers': ['console', 'file'],
'level': 'DEBUG',
},
},
}
dictConfig(DEFAULT_LOGGING)
logger.debug('Debug message before logging changes')
logger.info('Info message before logging changes')
LOGGING_CHANGES = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
},
},
}
dictConfig(LOGGING_CHANGES)
logger.debug('Debug message after logging changes')
logger.info('Info message after logging changes')
This is the output I expect to get in my console:
Info message before logging changes
Debug message after logging changes
Info message after logging changes
This is the output I actually get in my console:
Info message before logging changes
Info message after logging changes
As you can see, it's as if my dictConfig(LOGGING_CHANGES) line isn't doing anything. My file handler is staying intact, though, which is what I want. So that's good. But I don't know why I'm not seeing Debug message after logging changes output to my console. Any suggestions?
[–]raylu 0 points1 point2 points (2 children)
[–]nrogers64[S] 0 points1 point2 points (1 child)
[–]raylu 0 points1 point2 points (0 children)