logging: repeated messages

Peter Otten __peter__ at web.de
Wed Nov 19 05:18:27 EST 2003


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




More information about the Python-list mailing list