[ python-Bugs-1282539 ] logging.shutdown() not correct for chained handlers

SourceForge.net noreply at sourceforge.net
Wed Sep 7 05:12:52 CEST 2005


Bugs item #1282539, was opened at 2005-09-05 16:12
Message generated for change (Settings changed) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1282539&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Fons Dijkstra (fdij)
>Assigned to: Vinay Sajip (vsajip)
Summary: logging.shutdown() not correct for chained handlers

Initial Comment:
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()


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1282539&group_id=5470


More information about the Python-bugs-list mailing list