Logging
Samuel Kilchenmann
skilchen at hotmail.ch
Thu Feb 3 01:15:31 EST 2005
"Neil Benn" <benn at cenix-bioscience.com> schrieb im Newsbeitrag
news:mailman.1750.1107345636.22381.python-list at python.org...
> Neil Benn wrote:
>
>> Hello,
>>
>> I'm running a test and having issues with logging, if I call
>> logging.shutdown() and then want to start the logging going again
>> then I get a problem as if I call shutdown, I can't get the root
>> logger again, such as :
>>
>> .<snip>
>
> Previous code missed out a line :
>
> .>>> import logging
> .>>> objTestLogger = logging.getLogger()
> .>>> objTestLogger.setLevel(logging.INFO)
> .>>> objTestLogger.addHandler(logging.FileHandler('c:\\test.log'))
> .>>> objTestLogger.info("THIS IS A TEST")
> .>>> logging.shutdown()
> .>>> objTestLogger = logging.getLogger()
> .>>> objTestLogger.setLevel(logging.INFO)
> .>>> objTestLogger.addHandler(logging.FileHandler('c:\\test.log'))
> .>>> objTestLogger.info("THIS IS A TEST")
> .Traceback (most recent call last):
> . File "c:\program files\python23\lib\logging\__init__.py", line 679,
> in emit
> . self.stream.write("%s\n" % msg)
> .ValueError: I/O operation on closed file
> .>>>
>
logging.shutdown() doesn't clean up the dict of handlers at the module
level and the list of handlers maintained and at the level of the
manager of loggers. So after calling logging.getLogger() etc. for the
second time, you still have the closed filehandle in the list of
loghandlers. The new FileHandler with the open filehandle is appended to
this list of handlers. Then when you call ...info("...") this call is
dispatched to all handlers in the handlerlist, i.e. also to the one with
the closed filehandle.
I don't know if this has to be considered a bug in the logging module or
if your use case is simply not covered.
If you add something like
.>>> logging._handlers.clear()
.>>> logging.root.handlers = []
.>>> for l in logging.Logger.manager.loggerDict.values():
.>>> l.handlers = []
after logging.shutdown() and before getting the new logger, your script
will probably run without errors.
hth,
Samuel
More information about the Python-list
mailing list