Message26215
Consider the following code:
import logging
import logging.handlers
if __name__ == "__main__":
fh = logging.handlers.RotatingFileHandler("log.txt")
mh = logging.handlers.MemoryHandler(1024,
logging.ERROR, fh)
logging.getLogger().addHandler(mh)
logging.getLogger().warning("next statement crashes")
logging.shutdown()
which results in a crash due to operating on a closed
file. The reason is that the shutdown flushes and closes
all handlers in undefined order. In above case, first
the 'fh' handlers is flushes and closed then the 'mh'
handler.
The solution is to first flush all handlers times the
number of handlers before closing them. Thus (omitting
error handling):
def shutdown():
for i in range(len(_handlers)):
for h in _handlers.keys():
h.flush()
for h in _handlers.keys():
h.close()
|
|
Date |
User |
Action |
Args |
2007-08-23 14:34:15 | admin | link | issue1282539 messages |
2007-08-23 14:34:15 | admin | create | |
|