logging module: removing handlers

M

Michele Simionato

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
 
D

Diez B. Roggisch

Michele said:
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
 
M

Michele Simionato

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
 
D

Diez B. Roggisch

Michele said:
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
 
M

Michele Simionato

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top