python logging

I would like customizable logging in python applications, and I would like to easily send log messages to multiple handlers without any modification of the application code. The built-in logging module provides a very robust and easy-to-use logging capability.

In it's simplest form, log messages will be sent to the console with minimal formatting, e.g.,

>>> import logging
>>> 
>>> logging.warning('this is a warning')
WARNING:root:this is a warning

In fact, your application and module code can simply use the built-in logging methods which can inherit logging handlers set by the main application code.

Below is an example of a logging configuration that creates three logging handlers:

import logging

# Log to file
logging.basicConfig(
    filename='test.log',
    level=logging.INFO,
    format='%(asctime)-15s %(levelname)s:%(filename)s:%(lineno)d -- %(message)s'
)   

# Log to console
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
console.setFormatter(logging.Formatter('%(levelname)s:%(filename)s:%(lineno)d -- %(message)s'))
logging.getLogger().addHandler(console)

# Log to syslog
from logging.handlers import SysLogHandler
syslog = SysLogHandler(address='/dev/log')
syslog.setFormatter(logging.Formatter('%(asctime)-15s %(levelname)s:%(filename)s:%(lineno)d -- %(message)s'))
logging.getLogger().addHandler(syslog)

Once this module is imported, any logging calls to the root logger [e.g., logging.warning(), logging,critical(), etc] will be processed by all three handlers. You can add as many log handlers as needed while your module code maintains a very simple logging interface, e.g.,

import logging

logging.debug('this is a debug msg')
logging.info('this is an info msg')
logging.warning('this is a warning msg')
logging.error('this is an error msg')
logging.critical('this is a critical error msg')

The console output will look like:

INFO:test_log.py:4 -- this is an info msg
WARNING:test_log.py:5 -- this is a warning msg
ERROR:test_log.py:6 -- this is an error msg
CRITICAL:test_log.py:7 -- this is a critical error msg

The filelog (and syslog) will look like:

 
2011-09-26 12:30:52,521 INFO:test_log.py:4 -- this is an info msg
2011-09-26 12:30:52,522 WARNING:test_log.py:5 -- this is a warning msg
2011-09-26 12:30:52,522 ERROR:test_log.py:6 -- this is an error msg
2011-09-26 12:30:52,522 CRITICAL:test_log.py:7 -- this is a critical error msg
This entry was posted in python, software arch.. Bookmark the permalink.

Comments are closed.