Problem with logging module

Diez B. Roggisch deetsNOSPAM at web.de
Thu Oct 14 08:09:50 EDT 2004


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")

------


-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list