Help with logging module

Steven Taschuk staschuk at telusplanet.net
Sat May 17 13:00:50 EDT 2003


Quoth Kenneth Pronovici:
> I've got a somewhat complicated logging scenario that I'm hoping someone
> could help me sort out.
  [...]
> 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.

This is the way to do it.  Example:

    import logging
    import sys

    applog = logging.getLogger('app.log')
    stdouthandler = logging.StreamHandler(sys.stdout)
    stderrhandler = logging.StreamHandler(sys.stderr)
    applog.addHandler(stdouthandler)
    applog.addHandler(stderrhandler)

    applog.setLevel(logging.INFO)
    stdouthandler.setLevel(logging.INFO)
    stderrhandler.setLevel(logging.ERROR)

    applog.error('error') # appears on stdout and stderr
    applog.info('info')   # appears only on stdout
    applog.debug('debug') # appears neither on stdout nor on stderr

> 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?  

It's hard to say, without seeing some code that doesn't do what
you expect.

-- 
Steven Taschuk             "[W]e must be very careful when we give advice
staschuk at telusplanet.net    to younger people: sometimes they follow it!"
                             -- "The Humble Programmer", Edsger Dijkstra





More information about the Python-list mailing list