471,348 Members | 1,601 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Logging module gives duplicate log entries

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

Aug 21 '07 #1
5 15041
On 8/21/07, Shiao <mu*******@gmail.comwrote:
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.
Aug 21 '07 #2
>
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.

Aug 21 '07 #3
Shiao wrote:
>You need to remove the handler from the logging object

# remove the handler once you are done
applog.removeHandler(hdl)
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:
>>import logging
logging.basicConfig()
root = logging.getLogger()
root.warn("once")
WARNING:root:once
>>handler = logging.StreamHandler()
root.addHandler(handler)
root.warn("twice")
WARNING:root:twice
twice
>>root.removeHandler(handler)
root.warn("once again")
WARNING:root:once 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
Aug 21 '07 #4
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.

Aug 21 '07 #5
Shiao wrote:
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.
>>import logging
for i in range(2):
.... logging.getLogger().addHandler(logging.StreamHandl er())
....
>>logging.warn("twice")
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.
>>import logging
for i in range(2):
.... logging.basicConfig()
....
>>logging.warn("once")
WARNING:root:once

Peter

Aug 22 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Tero Saarni | last post: by
3 posts views Thread by Irmen de Jong | last post: by
10 posts views Thread by Thomas Heller | last post: by
7 posts views Thread by Jed Parsons | last post: by
7 posts views Thread by Leo Breebaart | last post: by
12 posts views Thread by Tekkaman | last post: by
3 posts views Thread by seb | last post: by
3 posts views Thread by Ross Boylan | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.