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

SourceForge.net noreply at sourceforge.net
Mon Sep 19 03:01:40 CEST 2005


Bugs item #1282539, was opened at 2005-09-05 21:12
Message generated for change (Comment added) made by vsajip
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: Closed
>Resolution: Fixed
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()


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

>Comment By: Vinay Sajip (vsajip)
Date: 2005-09-19 01:01

Message:
Logged In: YES 
user_id=308438

Now checked into CVS.

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

Comment By: Vinay Sajip (vsajip)
Date: 2005-09-07 11:12

Message:
Logged In: YES 
user_id=308438

There is indeed a problem - thanks for the report. Rather
than your proposed solution, I would rather add another
internal list, _handlerList, which holds the handlers in
*reverse* order of creation. When shutting down, (a copy of)
this list is used for the iteration rather than
_handlers.keys().

I may remove _handlers in the future - I can't remember why
I made it a dict rather than a list.

Will check into CVS soon, then mark this as Fixed.

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

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