logging: repeated messages

T

Thomas Schulz

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')
 
P

Peter Otten

Thomas said:
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
 
P

Peter Otten

Peter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top