Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Logging module gives duplicate log entries

Reply
Thread Tools

Logging module gives duplicate log entries

 
 
Shiao
Guest
Posts: n/a
 
      08-21-2007
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

 
Reply With Quote
 
 
 
 
Amit Khemka
Guest
Posts: n/a
 
      08-21-2007
On 8/21/07, Shiao <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Shiao
Guest
Posts: n/a
 
      08-21-2007

>
> 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.

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      08-21-2007
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:rootnce
>>> handler = logging.StreamHandler()
>>> root.addHandler(handler)
>>> root.warn("twice")

WARNING:root:twice
twice
>>> root.removeHandler(handler)
>>> root.warn("once again")

WARNING:rootnce 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
 
Reply With Quote
 
Shiao
Guest
Posts: n/a
 
      08-21-2007
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.

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      08-22-2007
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:rootnce

Peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: python logging, handling multiline log entries Jean-Michel Pichavant Python 0 11-04-2010 11:57 AM
python logging, handling multiline log entries Jean-Michel Pichavant Python 0 11-04-2010 11:34 AM
Removing default logging handler (causes duplicate logging) Gal Aviel Python 1 03-04-2008 03:35 PM
Passing a log handle to a module? Help needed with logging module and rh0dium Python 2 08-04-2005 02:27 PM
Windows service making duplicate event log entries. Wade ASP .Net 3 07-19-2005 12:53 PM



Advertisments