Problem with logging module

Steve Erickson steve_erickson at hotmail.com
Fri Oct 15 15:37:59 CEST 2004


Thanks a bunch for your patience and explanations.  It would be nice
if there was a method of checking the logger or handler for an
instance of it:

logger = logging.getLogger(name)
if not logger.handler(dir + '/' + name + '.log'):
    logger.propagate = False
    handler = logging.FileHandler(dir + '/' + name + '.log')
    logger.addHandler(handler)

"Diez B. Roggisch" <deetsNOSPAM at web.de> wrote in message news:<cklqac$3il$02$1 at news.t-online.com>...
> Steve Erickson wrote:
> 
> > Thanks.  Coming from C++, I thought that the local logger instance in
> > each test method would go away when I went out of the context of the
> > method.  Sounds like the logging class in Python maintains artifacts
> > that affect instantiations in other methods.  Or maybe I'm confused
> > about how Python handles local versus class variables.
> 
> Nothing to do with python - its the way logging is designed, and it makes
> sense: if at one place you attach the logger named foo.bar to a handler,
> and at another place obtain a reference to foo.bar to log some stuff, you
> don't want to reiterate the whole handler-attaching stuff.
> 
> So you simply abused the logging module - just set up the logger once, and
> then obtain a reference to it using logging.getLogger whenever you need it
> - that totally rids you of your logger class, that so far doesn't do much
> anyway. Like this:
> 
> ------
> logger = logging.getLogger(name)
> logger.propagate = False
> handler = logging.FileHandler(dir + '/' + name + '.log')
> logger.addHandler(handler)
> 
> class Test:
> 
>     def test_me(self):
>         logger = logging.getLogger(name)
>         logger.debug("test_me")
> 
> ------



More information about the Python-list mailing list