Richard Jones' Log: Simple usage of Python's logging module
Several people have posted about Python's new logging module. Most are confused by its complexity and inaccessible documentation (great reference, where's the introduction?) Well, I've used it once now, and here's the simplest I could make it:
import logging logger = logging.getLogger('myapp') hdlr = logging.FileHandler('var/myapp.log') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.INFO)
And then to use it:
logger.info('a log message')
... of course, for most people, the ideal usage would be:
import logging logger = logging.open('var/myapp.log')
Gee, that'd be nice. Maybe I'll submit that as a patch, in one of my spare moments.