2011 / 09 / 18
How to change logging.basicConfig configuration
In Python, when you need a logging mechanism, logging.basicConfig is very convenient: With one line of code, you can setup a basic logging system which is sufficient for most simple scripts.Once setup, changing its configuration is easy but not well documented. The key is that logging.basicConfig sets up an anonymous Logger object that you can then access with logging.getLogger. Here is an example where we change the logging level after the setup:
#!/usr/bin/python
import logging
import time
# basic setup with ISO 8601 time format
logging.basicConfig(filename='/tmp/test.log', format='%(asctime)sZ pid:%(process)s module:%(module)s %(message)s', level=logging.INFO)
# switch to UTC / GMT
logging.Formatter.converter = time.gmtime
logging.info('blah blah')
logging.debug('you won\'t see this')
# change the logging level
logging.getLogger().setLevel(logging.DEBUG)
logging.debug('you should see this')
Copyright Yves Dorfsman, 2011.
Yves Dorfsman is an python contractor in Calgary.
atom feed