473,799 Members | 3,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.basicCo nfig(level=logg ing.DEBUG, filename='/tmp/foo.log')

But this results in two entries for each logged event:

applog = logging.getLogg er()
applog.setLevel (logging.DEBUG)
hdl = logging.FileHan dler('/tmp/foo.log')
applog.addHandl er(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.getLogg er('foo').

Any clue would be more than welcome.

best,
ShiaoBu

Aug 21 '07 #1
5 15621
On 8/21/07, Shiao <mu*******@gmai l.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.basicCo nfig(level=logg ing.DEBUG, filename='/tmp/foo.log')

But this results in two entries for each logged event:

applog = logging.getLogg er()
applog.setLevel (logging.DEBUG)
hdl = logging.FileHan dler('/tmp/foo.log')
applog.addHandl er(hdl)
You need to remove the handler from the logging object

# remove the handler once you are done
applog.removeHa ndler(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.removeHa ndler(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.removeH andler(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.basicCo nfig() implicitly adds a handler and you add
another one calling addHandler(). Let's use a StreamHandler to see the
effect immediately:
>>import logging
logging.basic Config()
root = logging.getLogg er()
root.warn("on ce")
WARNING:root:on ce
>>handler = logging.StreamH andler()
root.addHandl er(handler)
root.warn("tw ice")
WARNING:root:tw ice
twice
>>root.removeHa ndler(handler)
root.warn("on ce again")
WARNING:root:on ce 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.getLogg er()
applog.setLevel (logging.DEBUG)
hdl = logging.FileHan dler('/tmp/foo.log')
applog.addHandl er(hdl)

However if I REPLACE the above by:

logging.basicCo nfig(level=logg ing.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.getLogg er()
applog.setLevel (logging.DEBUG)
hdl = logging.FileHan dler('/tmp/foo.log')
applog.addHandl er(hdl)

However if I REPLACE the above by:

logging.basicCo nfig(level=logg ing.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.getLogg er().addHandler (logging.Stream Handler())
....
>>logging.warn( "twice")
twice
twice

logging.basicCo nfig() 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.basicCo nfig()
....
>>logging.warn( "once")
WARNING:root:on ce

Peter

Aug 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2300
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 that was issuing the library call. This is similar to what Logging itself does since it doesn't print "logging/__init__.py" as value of %(filename) but instead it prints users file where logging method (info, error...) was called. What would be...
3
2264
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 timestamp (no date). Is there an easy way to change this? How can I make %(asctime) dump only the time? I'm using a configuration file to set up the logging. Which brings me to another thing.
10
3305
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 runs? I would like to setup the logging so that there is no logging when nothing is configured, and no warning messages are printed. Thomas
7
2431
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 with the timestamp the same down to the millisecond. Is this something I'm doing wrong?
7
1548
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. The situation is this: I am writing fairly large console scripts in Python. They have quite a few command-line options, which lead to configuration variables that are needed all over the program (e.g. the "--verbose" option alone is used by just...
12
1979
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 1072 as the line number. I set up my config as follows: logBaseConf = { 'level' : logging.DEBUG,
3
1609
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 logging function (so they appear on the consile) and they are called once only. Obviously there is some understantding of the logging module that I am missing. My simple logging program (see below) is called by several processes. In this way I...
3
4300
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 the logging module's facilities. The logging module itself is "thread-safe", but I think that just means that individual output is protected. If I have, in temporarly sequence:
3
11567
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 logging system will create a default logger that *also* emits logs, which I can't seem to get rid of. Is there a way I can suppress the creation of this default logger, or remove it when I 'm setting up my handlers? Thanks. Sample code:
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9079
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.