Help with logging module

Kenneth Pronovici pronovic at skyjammer.com
Fri May 16 19:51:42 EDT 2003


I've got a somewhat complicated logging scenario that I'm hoping someone
could help me sort out.

I've converted an existing app to use the logging module, and now I'm
trying to figure out how to make log output go to the right places.  

All of the modules log to a target something like "app.log" or
"app.output".  The "app.log" messages are logging and debugging
messages, and the "app.output" messages are stdout and stderr generated
by sub-commands (i.e. tar).  

Here are the output rules:

   o I always want "app.log" ERROR, WARN and INFO to be written to disk
   o I always want "app.log" ERROR to go to the screen  
   o If --output, I want "app.output" DEBUG to be written to disk
   o If --debug, I want "app.log" DEBUG to be written to disk
   o If --verbose, I want "app.log" INFO to go to the screen
   o If --verbose AND --debug, I want "app.log" DEBUG to go to the screen

I'm trying to set all of this up programmatically, rather than with a
config file, since the rules will never vary from what's above, and I
want everything encapsulated within my high-level script.  I've tried it
a few ways already, and I can never quite get it right.

For my first pass, I called getLogger() to get three different loggers,
one for "app.log" on disk, one for "app.log" to the screen, and one for
"app.output" to disk, and set up handlers and formatters for each.  This
works for some of the output rules, but I don't seem to be able to set
logLevel() independently for each of the three loggers.  For instnace,
setting DEBUG for the "app.log" logger that logs to disk also causes
debugging output to go the screen.  (I assume this is because the two
"app.log" loggers reference the same object).

For my second pass, I tried calling getLogger() once for "app.log" and
once for "app.output".  Then, I created several different handlers, one
for log messages to disk, one for log messages to screen, and one for
output to disk, and I tried setting the logLevel() just on the handlers
rather than on the loggers themselves.  This didn't get my anywhere,
though - I don't get any output at all.

I'm sure this can be done, but I'm not sure what I'm missing.  Can
someone point out where I've gone wrong here?  

KEN

-- 
Kenneth J. Pronovici <pronovic at ieee.org>




More information about the Python-list mailing list