log messages appearing twice with Python Logging

后端 未结 8 1371
天命终不由人
天命终不由人 2020-12-04 18:39

I\'m using Python logging, and for some reason, all of my messages are appearing twice.

I have a module to configure logging:

# BUG: It\'s outputting         


        
相关标签:
8条回答
  • 2020-12-04 19:31

    If you are seeing this problem and you're not adding the handler twice then see abarnert's answer here

    From the docs:

    Note: If you attach a handler to a logger and one or more of its ancestors, it may emit the same record multiple times. In general, you should not need to attach a handler to more than one logger - if you just attach it to the appropriate logger which is highest in the logger hierarchy, then it will see all events logged by all descendant loggers, provided that their propagate setting is left set to True. A common scenario is to attach handlers only to the root logger, and to let propagation take care of the rest.

    So, if you want a custom handler on "test", and you don't want its messages also going to the root handler, the answer is simple: turn off its propagate flag:

    logger.propagate = False
    
    0 讨论(0)
  • 2020-12-04 19:31

    The handler is added each time you call from outside. Try Removeing the Handler after you finish your job:

    self.logger.removeHandler(ch)
    
    0 讨论(0)
提交回复
热议问题