logging.shutdown() ValueError: I/O operation on closed file

Peter Otten __peter__ at web.de
Thu Nov 13 18:54:19 EST 2003


j vickroy wrote:

> I'm trying to understand the behavior of the Python 2.3 logging module (MS
> Windows 2k) with regard to RotatingFileHandler.  The following script
> illustrates a puzzling problem.  What is wrong with this script?

>    if handler:
>       handler.flush()
>       handler.close()
>       logger.removeHandler(handler)

The handler is stored in the logging._handlers dictionary in order to close
it when shutdown() is called. But you already did close it manually.
I think you have three options to fix your script.

(1) Don't call shutdown() at all and manually close the last handler
instead.

(2) Change the above to

if handler:
    logger.removeHandler(handler)
    handler.flush()
    handler.close()
    del logging._handlers[handler]

so that shutdown() cannot touch closed handlers, or

(3) don't close handlers manually

if handler:
    logger.removeHandler(handler)

so that shutdown() gets a still open handler as expected. I would go with
the latter, as it does not rely on implementation details.

(all untested, use at your own risk)

Peter







More information about the Python-list mailing list