duplicate output in simple python logging configuration

前端 未结 3 2065
刺人心
刺人心 2020-12-09 16:20

I\'m setting up python logging as follows:

def setup_logging():
    loggers = (logging.getLogger(\"amcat\"), logging.getLogger(\"scrapers\"),logging.getLogge         


        
相关标签:
3条回答
  • 2020-12-09 16:27

    Basically, when one of your child logger displays a message, it goes backwards in the hierarchy, and the parents are also logging the same thing.

    To cancel that behavior, you can add this:

    logger.propagate = False
    

    When it hits the child, it won't hit the parent afterwards.

    Here is some documentation about this behavior.

    0 讨论(0)
  • 2020-12-09 16:36

    I figured this out, thanks Paco for pointing me in the right direction

    it turns out that when getLogger is called, handlers are added to it:

    >>> print(effectivehandlers(logger))
    [<logging.StreamHandler object at 0x305ad90>, <logging.FileHandler object at 0x305ae10>]
    >>> logging.getLogger(name) #the same logger
    <logging.Logger object at 0x7fa08fb9b2d0>
    >>> print(effectivehandlers(logger))
    [<logging.StreamHandler object at 0x305ad90>, <logging.FileHandler object at 0x305ae10>, <logging.StreamHandler object at 0x305ad90>, <logging.FileHandler object at 0x305ae10>]
    

    Now, both the child and the parent have the same handlers. Hence duplicate outputs.

    0 讨论(0)
  • 2020-12-09 16:44

    for python 2.7

    logging.handlers.pop()
    

    Reference by user "radtek": Python Logging - Messages appearing twice

    0 讨论(0)
提交回复
热议问题