473,387 Members | 1,534 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,387 software developers and data experts.

logging: repeated messages

Hello,

I'm using the logging package from python 2.3 on RH linux.

Everything in the test program below works fine if we just use the log
configuration with 'logging.basicConfig'. For the example below we get
the expected output:
ERROR:loggerName:error1 msg
ERROR:loggerName:error2 msg

If we add a StreamHandler (uncomment the lines with 'hdlr'), things get
strange: The messages are repeated as many times as there were messages
before that call that qualified for output by their loglevel.
Output with enabled StreamHandler:
error1 msg
error1 msg
ERROR:loggerName:error1 msg
error2 msg
error2 msg
error2 msg
ERROR:loggerName:error2 msg
The problem in the sample program might be the creation of the logger
object in each 'log' call.
One of the constraints for the use of the loggger in that way is because
I have to provide a simple procedure 'log' to handle everything. Sure,
if I would create the logger only once it would be fine.

But why does the configuration created with basicConfig work fine?

Any sugestions to resolve the problem?

--Thomas

#!/usr/bin/env python

import logging, logging.handlers

def log(level, message):

logging.basicConfig()
logger = logging.getLogger('loggerName')

# Streamhandler, uncomment the following 3 lines
hdlr = logging.StreamHandler()
hdlr.setLevel(logging.WARN)
logger.addHandler(hdlr)

if level == 'DEBUG':
logger.debug(message)
elif level == 'INFO':
logger.info(message)
elif level in ['WARN', 'WARNING']:
logger.warn(message)
elif level == 'ERROR':
logger.error(message)
elif level == 'CRITICAL':
logger.critical(message)

log('INFO', 'info message')
log('ERROR', 'error1 msg')
log('ERROR', 'error2 msg')

Jul 18 '05 #1
2 2851
Thomas Schulz wrote:
Hello,

I'm using the logging package from python 2.3 on RH linux.

Everything in the test program below works fine if we just use the log
configuration with 'logging.basicConfig'. For the example below we get
the expected output:
ERROR:loggerName:error1 msg
ERROR:loggerName:error2 msg

If we add a StreamHandler (uncomment the lines with 'hdlr'), things get
strange: The messages are repeated as many times as there were messages
before that call that qualified for output by their loglevel.
Output with enabled StreamHandler:
error1 msg
error1 msg
ERROR:loggerName:error1 msg
error2 msg
error2 msg
error2 msg
ERROR:loggerName:error2 msg
The problem in the sample program might be the creation of the logger
object in each 'log' call.
One of the constraints for the use of the loggger in that way is because
I have to provide a simple procedure 'log' to handle everything. Sure,
if I would create the logger only once it would be fine.

But why does the configuration created with basicConfig work fine?

Any sugestions to resolve the problem?

How about (untested):

def log(level, message, data=[]):
if len(data) == 0:
logging.basicConfig()
logger = loggin.getLogger('name')
hdlr = logging.StreamHandler()
hdlr.setLevel(logging.WARN)
logger.addHandler(hdlr)
data.append(hdlr)

if level == "DEBUG":
#more stuff

The StreamHandler would only be created once, if you never provide an
explicit data argument. If the state to be kept gets more complicated, use
class instead (untested):

class Log:
def __init__(self):
logging.basicConfig()
logger = loggin.getLogger('name')
self.hdlr = logging.StreamHandler()
self.hdlr.setLevel(logging.WARN)
logger.addHandler(self.hdlr)
def __call__(self, level, message):
if level == "DEBUG":
#more stuff
log = Log()
log("DEBUG", "some message")

HTH,
Peter

Peter
Jul 18 '05 #2
Peter Otten wrote:
How about (untested):

def log(level, message, data=[]):
if len(data) == 0:
logging.basicConfig()
logger = loggin.getLogger('name')
hdlr = logging.StreamHandler()
hdlr.setLevel(logging.WARN)
logger.addHandler(hdlr)
data.append(hdlr)


Oops, no need to store the StreamHandler,

if data:
...
data.append(1)

should have the same effect. As you don't use the newly created handler in
subsequent calls, I would recommend to move the code inside the if
statement into another function that is only called once at startup.

Peter

Jul 18 '05 #3

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

Similar topics

0
by: max | last post by:
I have an application which calls a module runx, in runx I import logging and get a logger add handlers. I want to be able to rename the file created by fileHandler. several problems, I can't...
0
by: Robert.Schmitt | last post by:
I found that the configuration system of the new logging package of Python 2.3 has some unintuitive idiosyncracies that are worth mentioning because they can cost you quite some development time...
1
by: Jim | last post by:
Hi, with the logging module, I am using the FileHandler formatter. Is there a setting to stop the logger from putting the same message more than once? So it would say: Message whatever...
1
by: j vickroy | last post by:
My system: MSW XP professional Python 2.3.3 logging package: 0.4.9.2 My problem: The log_test3.py script, provided with the logging package distribution, generates an unexpected message: No...
1
by: Ajay | last post by:
hi! in my web app. i log debug messages to a file. however, sometimes the messages appear more than once even though the code in which the log statement appears gets executes only once i have a...
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...
5
by: Dilip | last post by:
I have a tiny design question to ask. I am stuck with a home-brewed logging library that has different methods for logging messages based on severity (like warning, error, information etc.). ...
5
by: Ritesh Raj Sarraf | last post by:
import os, sys, logging logger = logging.getLogger("my_app") conerr = logging.StreamHandler(sys.stderr) conerr.setLevel(logging.DEBUG) conerr_formatter = logging.Formatter('%(levelname)s...
6
by: Larry Bates | last post by:
Every time I look at the logging module (up until now) I've given up and continue to use my home-grown logger that I've been using for years. I'm not giving up this time ;-) I find that I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.