Loggers and reloads

Francois Bouffard fbouffar at gel.ulaval.ca
Wed May 26 15:01:08 EDT 2004


(Disclaimer: I'm new to both Python and OOP in general, and I may have
skipped a few lines in the docs)

I'm using the logging module inside one of my module. I instanciate a
logger which uses a StreamHandler to output to sys.stdout. It works
really well; however, I mainly use my module in the Python interpreter
in an interactive way, and I often have to reload said module.

The problem is that each time I reload my module, a new logger object
seems to be created, and the old one is not deleted. Both the new and
the old object are still working, so that each log message is
repeated; in general, if I did N reloads of my module, each message is
repeated N+1 times.

The del statement seems to only delete references, so that it can't be
used to get rid of the old logger objects. Maybe the
logging.shutdown() function is supposed to be of some help here, but I
don't see how to use it. Any hint as to what I'm doing wrong or what I
should do is welcome.

Here's some sample module code:

module logtest.py
---------------
import sys, logging

LOGLEVEL = logging.INFO
loghandler = logging.StreamHandler(sys.stdout)
logformat = logging.Formatter('%(name)s :: %(message)s')
loghandler.setFormatter(logformat)
logger = logging.getLogger('LoggerExample')
logger.addHandler(loghandler)
logger.setLevel(LOGLEVEL)

def info_message():
    logger.info('This is an info-level message')
---------------

>>> import logtest
>>> logtest.info_message()
LoggerExample :: This is an info-level message
>>> reload(logtest)
<module 'logtest' from 'logtest.pyc'>
>>> logtest.info_message()
LoggerExample :: This is an info-level message
LoggerExample :: This is an info-level message



More information about the Python-list mailing list