473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 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 15529
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tero Saarni | last post by:
I'm using Logging in my library module for producing trace log of certain events. For being more useful these log entries should be associated with the filename and line number of *users* code...
3
by: Irmen de Jong | last post by:
Hello I'm using the logging module in Python 2.3.3, with a format string containing %(asctime). But it now dumps a full date +timestamp in the log, which is nice but sometimes I only want the...
10
by: Thomas Heller | last post by:
I'm about to add some logging calls to a library I have. How can I prevent that the script that uses the library prints 'No handlers could be found for logger "comtypes.client"' when the script...
7
by: Jed Parsons | last post by:
Hi, I'm using the logging module for the first time. I'm using it from within Zope Extensions. My problem is that, for every event logged, the logger is producing multiple identical entries...
7
by: Leo Breebaart | last post by:
I have another question where I am not so much looking for a solution but rather hoping to get some feedback on *which* solutions people here consider good Pythonic ways to approach a issue. ...
12
by: Tekkaman | last post by:
I'm getting a strange behaviour from the "pathname" and "lineno" formatter mapping keys. Instead of my file and my line number I get: /usr/lib/python2.4/logging/__init__.py as the file, and...
3
by: seb | last post by:
Hi, I am writing to a file some basic information using the logging module. It is working but in the log file some line are printed several time. I had put some print debugging messages in the...
3
by: Ross Boylan | last post by:
I would like my different threads to log without stepping on each other. Past advice on this list (that I've found) mostly says to send the messages to a Queue. That would work, but bypasses...
3
by: Chris Shenton | last post by:
I am setting up handlers to log DEBUG and above to a rotating file and ERROR and above to console. But if any of my code calls a logger (e.g., logging.error("foo")) before I setup my handlers, the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.