logging module: removing handlers

Diez B. Roggisch deets at nospam.web.de
Wed Nov 21 04:23:56 EST 2007


Michele Simionato schrieb:
> I have just discovered a bug in my code using the logging module, due
> to
> handlers not being closed properly. The issue was that I called the
> function
> removeHandler and I assumed that it took care of closing the handler,
> but it did not.
> Looking at the source code of logging/__init__.py I discovered that it
> is
> implemented as follows:
> 
>     def removeHandler(self, hdlr):
>         """
>         Remove the specified handler from this logger.
>         """
>         if hdlr in self.handlers:
>             #hdlr.close()
>             hdlr.acquire()
>             try:
>                 self.handlers.remove(hdlr)
>             finally:
>                 hdlr.release()
> 
> The question is: why in the hell the "hdlr.close()" line is
> commented??
> I discovered this because we had locks in our production database
> (we were logging on the DB) and it was not easy to spot the source of
> the problem, so I would like to know the rationale for not closing
> the handler in removeHandler.

I can only guess - but I'd say if you can do

foo.removeHandler(h)

you can do

foo.removeHandler(h)
h.close()

easily. But not

foo.removeHandler(h) # implicit closing
bar.addHandler(h)

It does kind of make sense if you decouple the life-cycles IMHO.

Diez



More information about the Python-list mailing list