Logging module gives duplicate log entries

S

Shiao

Hi,
I am getting duplicate log entries with the logging module.

The following behaves as expected, leading to one log entry for each
logged event:

logging.basicConfig(level=logging.DEBUG, filename='/tmp/foo.log')

But this results in two entries for each logged event:

applog = logging.getLogger()
applog.setLevel(logging.DEBUG)
hdl = logging.FileHandler('/tmp/foo.log')
applog.addHandler(hdl)


The app is based on the web.py framework, so I guess my problem may
be
connected to be some interaction with other uses of logging within
the
framework. This is not specific to the root logger, the same happens
with logging.getLogger('foo').

Any clue would be more than welcome.

best,
ShiaoBu
 
A

Amit Khemka

Hi,
I am getting duplicate log entries with the logging module.

The following behaves as expected, leading to one log entry for each
logged event:

logging.basicConfig(level=logging.DEBUG, filename='/tmp/foo.log')

But this results in two entries for each logged event:

applog = logging.getLogger()
applog.setLevel(logging.DEBUG)
hdl = logging.FileHandler('/tmp/foo.log')
applog.addHandler(hdl)

You need to remove the handler from the logging object

# remove the handler once you are done
applog.removeHandler(hdl)


Cheers,
amit.

----
Amit Khemka
website: www.onyomo.com
wap-site: www.owap.in
Home Page: www.cse.iitd.ernet.in/~csd00377

Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.
 
S

Shiao

You need to remove the handler from the logging object

# remove the handler once you are done
applog.removeHandler(hdl)

Cheers,
amit.

I'm not sure how this could help.
 
P

Peter Otten

I'm not sure how this could help.

If you have multiple handlers you'll get a logged message for every handler.

In your code logging.basicConfig() implicitly adds a handler and you add
another one calling addHandler(). Let's use a StreamHandler to see the
effect immediately:
WARNING:root:twice
twice
WARNING:root:eek:nce again

Of course it would be preferable not to add a second handler in the first
place, either by omitting the basicConfig() or the explicit addHandler()
call.

Peter
 
S

Shiao

Maybe my question wasn't very clear. What I meant is that these four
lines lead in my case to two entries per logged event:

applog = logging.getLogger()
applog.setLevel(logging.DEBUG)
hdl = logging.FileHandler('/tmp/foo.log')
applog.addHandler(hdl)

However if I REPLACE the above by:

logging.basicConfig(level=logging.DEBUG, filename='/tmp/foo.log')

things work as expected.
 
P

Peter Otten

Shiao said:
Maybe my question wasn't very clear. What I meant is that these four
lines lead in my case to two entries per logged event:

applog = logging.getLogger()
applog.setLevel(logging.DEBUG)
hdl = logging.FileHandler('/tmp/foo.log')
applog.addHandler(hdl)

However if I REPLACE the above by:

logging.basicConfig(level=logging.DEBUG, filename='/tmp/foo.log')

things work as expected.

Then you have a logic error in your program that causes that piece of code
to run twice (I simulate that by the for-loop):

$ python
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information..... logging.getLogger().addHandler(logging.StreamHandler())
....twice
twice

logging.basicConfig() on the other hand does nothing if it finds existing
handlers:

$ python
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information..... logging.basicConfig()
....WARNING:root:eek:nce

Peter
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top