[py-dev] py.log -- annotating logs

Ian Bicking ianb at colorstudy.com
Wed Jul 27 18:51:39 CEST 2005


I haven't actually used py.log yet, but was just doing logging in 
another context, and I'm curious if py.log solves the problem, or if it 
could be made to do so.

Specifically, I want to add contextual information to all my logging. 
In a web app that might be the remote IP address, session ID, or 
username.  Other environments usually have other context.  The actual 
information would probably live in threadlocal storage.  Looking at 
Grig's blog post, one way would be with a consumer that wraps another 
consumer.  But this seems like it would lead to potentially complex 
consumer composition, especially since there's a number of things that 
are possibly being pushed into consumers.

Maybe that's not so bad -- I personally am inclined toward that kind of 
composition anyway.  Really py.log could just be thin wrapper around 
record objects, where everything is dispatched to a single consumer. 
Like maybe I'd do:

def add_remote_ip(old_consumer):
     def new_consumer(message):
         message.remote_ip = get_current_remote_ip()
         message.format = '[%(keywords)s] %(time)s %(remote_ip)s 
%(message)s'
         return old_consumer(message)
     return new_consumer

py.log.set_consumer(annotate_consumer(py.log.get_consumer()))

message.format looks a little fragile, though.

-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org


More information about the py-dev mailing list