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 Python (2.7)
My understanding is basicConfig
simply sets up the root logger: so I'm attempting to do the same with the dictConfig
. That is, set up the root logger with a handler, and then all the loggers in my application will propagate up the root logger.
What's missing from the following snippet? The behaviour is the log file is created, but no output makes it. (I've tried various combos of setting levels, it doesn't appear to help)
import logging
log_dict = {
'loggers': {
'root': {
'handlers': ['file_handler']
}
},
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file_handler': {
'filename': 'c:/mylogfile.log',
'class': 'logging.FileHandler'
}
}
}
logging.config.dictConfig(log_dict)
logging.info('THIS IS A TEST')
logging.shutdown()
exit()
There are a couple of things wrong with your posted configuration:
'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:WARNING
, which means thatinfo()
messages won't get shown.The following code works perfectly for me:
And indeed, it's based on the answer mentioned above