How To Reconfigure A Logger Formatter When Using Dictconfig
I am using dictConfig to setup logging. One of the requirements I have is to change the default converter of the formatter (I am using a single formatter simpleFormatter for all my
Solution 1:
Here's an example of how to do it:
import logging
import logging.config
import time
classUTCFormatter(logging.Formatter):
converter = time.gmtimeLOGGING= {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'utc': {
'()': UTCFormatter,
'format': '%(asctime)s %(message)s',
},
'local': {
'format': '%(asctime)s %(message)s',
}
},
'handlers': {
'console1': {
'class': 'logging.StreamHandler',
'formatter': 'utc',
},
'console2': {
'class': 'logging.StreamHandler',
'formatter': 'local',
},
},
'root': {
'handlers': ['console1', 'console2'],
}
}
if__name__== '__main__':
logging.config.dictConfig(LOGGING)
logging.warning('Look out!')
When I run the above, I get:
2015-10-1712:20:36,217 Look out!
2015-10-1713:20:36,217 Look out!
The use of the '()'
key for custom instantiations is documented here in the paragraph beginning All other keys ...
.
Post a Comment for "How To Reconfigure A Logger Formatter When Using Dictconfig"