My first try using logger didn't work. Why?

Vinay Sajip vinay_sajip at yahoo.co.uk
Fri Jan 19 16:32:50 EST 2007


CedricCicada at gmail.com wrote:

> Vinay (or anybody else),
>
> Well, now that logging is working, how do I stop it from working?
>
> I'm developing in PythonWin.  When I run my test script, I get one
> message.  When I run it a second time, I get two, a third time gets me
> three, and so on.
>
> I feel like the Sorceror's Apprentice!
>
> Rob

The way to avoid this is to only call addHandler() once in your script.
PythonWin imports logging and keeps it in memory (something that
wouldn't happen if you ran the script repeatedly from the
command-line), so adding a handler every time results in multiple
handlers logging to the same event sink (e.g. NT Event Log).

So you need to use a flag to indicate whether initialisation has
already happened once, and avoid doing it again. I'm not sure of the
best way of doing this, since I don't know exactly how your app is set
up: if there's no more natural way, you could get a Logger instance and
see if it has any handlers added, before creating a new one and adding
it. This could be done by checking len(logger.handlers) == 0 for that
logger.

Best regards,


Vinay Sajip




More information about the Python-list mailing list