Basic Logging Dictconfig In Python
NOTE I'm aware of this answer but that doesn't work for me, I am hoping for a complete, self-contained working example. I'm trying to replace logging.basicConfig with dictConfig in
Solution 1:
There are a couple of things wrong with your posted configuration:
- A logger with a name of
'root'
is not the root logger. The root logger has a name of''
and is in general better configured using a'root'
entry outside the'loggers'
configuration, as described in this part of the logging documentation:
loggers - the corresponding value will be ...
...
root - this will be the configuration for the root logger. ...
- You haven't specified a logging level, so it remains at the default level of
WARNING
, which means thatinfo()
messages won't get shown.
Solution 2:
The following code works perfectly for me:
import logging
import logging.config
log_dict = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
},
},
'handlers': {
'default': {
'level': 'INFO',
'formatter': 'standard',
'class': 'logging.StreamHandler',
},
'file_handler': {
'level': 'INFO',
'filename': '/tmp/mylogfile.log',
'class': 'logging.FileHandler',
'formatter': 'standard'
}
},
'loggers': {
'': {
'handlers': ['file_handler'],
'level': 'INFO',
'propagate': True
},
}
}
logging.config.dictConfig(log_dict)
logging.info("test")
And indeed, it's based on the answer mentioned above
Post a Comment for "Basic Logging Dictconfig In Python"