Loggers and reloads

F

Francois Bouffard

(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')
 
D

Diez B. Roggisch

Francois said:
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 problem would disappear if you wrote a small testscript that uses your
module and execeted that from scratch, instead of reloading the module -
which might also create other state related problems.

But if you insist on using your module, you could place the logger
initialization into another module or encapsulate it in a function that you
call manually - but only once. As the logging systems state is kept in the
not-reloaded logging module, that should rid you of your problem. I bet it
would also be possible to check if for a given logger already a handler is
registered, and refuse to add another one.
 
?

=?ISO-8859-1?Q?Fran=E7ois_Bouffard?=

I bet it
would also be possible to check if for a given logger already a handler is
registered, and refuse to add another one.

You're completely right, thank you. There's always only one logger
object in my code, however, each time the module is reloaded, a new
handler is added. Therefore, something like:

for h in logger.handlers:
logger.removeHandler(h)

indeed does the trick if I put it before the logger.addHandler() call.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top