By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

logging module: removing handlers

P: n/a
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.

Michele Simionato
Nov 21 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Nov 21 '07 #2

P: n/a
On Nov 21, 10:23 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
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
But what is the use case for removing an handler without closing it?

Michele Simionato
Nov 21 '07 #3

P: n/a
Michele Simionato wrote:
On Nov 21, 10:23 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>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

But what is the use case for removing an handler without closing it?
Having thought more about this, it _has_ to be that way - think of one
handler attached to several loggers. Removing AND closing it from one would
render it useless for others. You can't want that to happen.

Diez
Nov 21 '07 #4

P: n/a
On Nov 21, 11:08 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Having thought more about this, it _has_ to be that way - think of one
handler attached to several loggers. Removing AND closing it from one would
render it useless for others. You can't want that to happen.
You have a point.
Nov 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.