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
    format='%(asctime)-15s %(levelname)s:%(filename)s:%(lineno)d -- %(message)s'

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

# 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'))

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')'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: -- this is an info msg -- this is a warning msg -- this is an error msg -- this is a critical error msg

The filelog (and syslog) will look like:

2011-09-26 12:30:52,521 -- this is an info msg
2011-09-26 12:30:52,522 -- this is a warning msg
2011-09-26 12:30:52,522 -- this is an error msg
2011-09-26 12:30:52,522 -- this is a critical error msg
This entry was posted in python. Bookmark the permalink.