Tuesday, January 01, 2008

Custom Python Logging Handler

Python has some nice basic logging facilities, but I really wanted a rotating logger that would rotate on each execution. I tried the obvious - just specifying class=MyModule.MyClassName, but I received a NameError exception for my trouble.

After some hacking on /python25/lib/logging (a big no-no!), I did another google search and discovered that I could just import the class in my application, and set a reference to my custom class. Hard-coding a logging handler doesn't feel very dynamic, but at least I did't have to hack any more.

from MyCustomHandlers import SessionRotatingFileHandler

logging.SessionRotatingFileHandler = SessionRotatingFileHandler
logfile_name = "test.l4p"

This allows me to place my custom log handler in my application directory. I still need to figure out how to add things to site-packages.

Labels: ,