Using logging module for conditional nested logs

Reckoner reckoner at gmail.com
Wed Nov 4 14:40:41 EST 2009


Hi,

I am getting started with your logging module and I went through the
tutorial and know-how to create a top-level 'root' logger with the
appropriate handlers.

I have a number of functions,say,

def foo1()

def foo2()
   ...
   foo1() # foo2 calls foo1

and I know how to connect each of these functions to the 'root' logger
by doing something like

def foo1()
  logger  = getLogger('root.foo1')

but I want to arrange it so that foo1 adds to the logfile that foo2 is
using ONLY when foo2 calls foo1. In other words, if I do

def foo2()
  logger  = getLogger('root.foo2')

or

def foo1()
  logger  = getLogger('root.foo2.foo1')

it responds to the 'root' logger when I want it to respond to *any*
logger that is attached to foo2.

Then, the question is how can I set up foo1 to log to whatever logger
foo2 is using. The purpose of doing this is that I am interested in
capturing when and by whom foo1 is called in whatever logger foo2 is
using. So, if I attach a separate logger to a third function, say, foo3
(), then I can avoid reporting those instances when foo3 calls foo1.

I hope that made some sense.





More information about the Python-list mailing list