logging: repeated messages

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Schulz

    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.basicC onfig'. For the example below we get
    the expected output:
    ERROR:loggerNam e:error1 msg
    ERROR:loggerNam e: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:loggerNam e:error1 msg
    error2 msg
    error2 msg
    error2 msg
    ERROR:loggerNam e: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.handler s

    def log(level, message):

    logging.basicCo nfig()
    logger = logging.getLogg er('loggerName' )

    # Streamhandler, uncomment the following 3 lines
    hdlr = logging.StreamH andler()
    hdlr.setLevel(l ogging.WARN)
    logger.addHandl er(hdlr)

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

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





  • Peter Otten

    #2
    Re: logging: repeated messages

    Thomas Schulz wrote:
    [color=blue]
    > 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.basicC onfig'. For the example below we get
    > the expected output:
    > ERROR:loggerNam e:error1 msg
    > ERROR:loggerNam e: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:loggerNam e:error1 msg
    > error2 msg
    > error2 msg
    > error2 msg
    > ERROR:loggerNam e: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?[/color]


    How about (untested):

    def log(level, message, data=[]):
    if len(data) == 0:
    logging.basicCo nfig()
    logger = loggin.getLogge r('name')
    hdlr = logging.StreamH andler()
    hdlr.setLevel(l ogging.WARN)
    logger.addHandl er(hdlr)
    data.append(hdl r)

    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.basicCo nfig()
    logger = loggin.getLogge r('name')
    self.hdlr = logging.StreamH andler()
    self.hdlr.setLe vel(logging.WAR N)
    logger.addHandl er(self.hdlr)
    def __call__(self, level, message):
    if level == "DEBUG":
    #more stuff
    log = Log()
    log("DEBUG", "some message")

    HTH,
    Peter

    Peter

    Comment

    • Peter Otten

      #3
      Re: logging: repeated messages

      Peter Otten wrote:
      [color=blue]
      > How about (untested):
      >
      > def log(level, message, data=[]):
      > if len(data) == 0:
      > logging.basicCo nfig()
      > logger = loggin.getLogge r('name')
      > hdlr = logging.StreamH andler()
      > hdlr.setLevel(l ogging.WARN)
      > logger.addHandl er(hdlr)
      > data.append(hdl r)[/color]

      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

      Comment

      Working...