logging: repeated messages

Thomas Schulz thomas at esands.com
Tue Nov 18 21:23:00 EST 2003


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









More information about the Python-list mailing list