python logging 确保只添加一次处理程序

标签 python logging

我有一段代码正在初始化一个记录器,如下所示。

logger = logging.getLogger()
hdlr = logging.FileHandler('logfile.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr) 
logger.setLevel(logging.DEBUG)

不幸的是,这段代码被多次调用,有什么方法可以检查处理程序是否已经存在 - 我更愿意在不必使用 Singleton 的情况下实现它。

编辑:抱歉,忘了提到这是在 python 2.5 上 - 干杯,理查德

最佳答案

作为@offbyone 评论,可以将冗余处理程序添加到记录器的同一实例。 python docs for logging说-

"Multiple calls to getLogger() with the same name will return a reference to the same logger object."

所以我们不必担心将实现变成单例,因为它已经是。

不幸的是,与同一记录器实例关联的处理程序不正确可能附加了重复的处理程序。

例子-

  1. 复制这段代码并保存在main.py中

    import logging
    print 'inside main.py',
    print '-'*50
    def logger():
    
          print 'initializing logger....'
          logPath = '.'
          fileName = 'temp'
    
          # configure log formatter
          logFormatter = logging.Formatter("%(asctime)s [%(filename)s] [%(funcName)s] [%(levelname)s] [%(lineno)d] %(message)s")
    
          # configure file handler
          fileHandler = logging.FileHandler("{0}/{1}.log".format(logPath, fileName))
          fileHandler.setFormatter(logFormatter)
    
          # configure stream handler
          consoleHandler = logging.StreamHandler()
          consoleHandler.setFormatter(logFormatter)
    
          # get the logger instance
          logger = logging.getLogger(__name__)
    
          # set the logging level
          logger.setLevel(logging.DEBUG)
    
          print 'adding handlers- '
    
          #if not len(logger.handlers):
          logger.addHandler(fileHandler)
          logger.addHandler(consoleHandler)
    
          print 'logger initialized....\n'
          print 'associated handlers - ', len(logger.handlers)
          for handler in logger.handlers:
                print handler
          print
          return logger
    
    main_logger = logger()
    main_logger.info('utilizing main.py logger.')
    print 'exiting main.py',
    print '-'*50
    
  2. 以及sub.py中的如下代码

    print 'inside sub.py',
    print '-'*50
    print 'importing main.py'
    import main
    print 'imported main.py'
    import logging
    print 'getting logger instance in sub'
    sub_logger = main.logger()
    print 'got logger instance in sub'
    sub_logger.info("utilizing sub_logger")
    print 'exiting sub.py',
    print '-'*50
    
  3. 运行 sub.py

    narayan@y510p:~/code/so$ python sub.py
    inside sub.py --------------------------------------------------
    importing main.py
    inside main.py --------------------------------------------------
    initializing logger....
    adding handlers- 
    logger initialized....
    
    associated handlers -  2
    <logging.FileHandler object at 0x7f7158740c90>
    <logging.StreamHandler object at 0x7f7158710b10>
    
    2015-08-04 07:41:01,824 [main.py] [<module>] [INFO] [41] utilizing main.py logger.
    exiting main.py --------------------------------------------------
    imported main.py
    getting logger instance in sub
    initializing logger....
    adding handlers- 
    logger initialized....
    
    associated handlers -  4 # <===== 4 handlers (duplicates added)
    <logging.FileHandler object at 0x7f7158740c90>
    <logging.StreamHandler object at 0x7f7158710b10>
    <logging.FileHandler object at 0x7f7158710bd0>
    <logging.StreamHandler object at 0x7f7158710c10>
    
    got logger instance in sub
    2015-08-04 07:41:01,824 [sub.py] [<module>] [INFO] [10] utilizing sub_logger
    2015-08-04 07:41:01,824 [sub.py] [<module>] [INFO] [10] utilizing sub_logger
    exiting sub.py --------------------------------------------------
    

因此,多次调用返回相同记录器的方法会添加重复的处理程序。

现在,你的问题-

is there any way I can check to see if the handler already exists

是的,有-

logger.handlers 返回与给定 logger 关联的所有处理程序的列表。

在将处理程序添加到记录器的实例之前,请确保不要添加重复的处理程序 在 main.py 中,取消注释 if not len(logger.handlers): 的行并正确缩进以下两行 -

if not len(logger.handlers):
    logger.addHandler(fileHandler)
    logger.addHandler(consoleHandler)

现在再次运行 sub.py

narayan@y510p:~/code/so$ python sub.py
inside sub.py --------------------------------------------------
importing main.py
inside main.py --------------------------------------------------
initializing logger....
adding handlers- 
logger initialized....

associated handlers -  2
<logging.FileHandler object at 0x7fd67a891c90>
<logging.StreamHandler object at 0x7fd67a862b10>

2015-08-04 08:14:45,620 [main.py] [<module>] [INFO] [41] utilizing main.py logger.
exiting main.py --------------------------------------------------
imported main.py
getting logger instance in sub
initializing logger....
adding handlers- 
logger initialized....

associated handlers -  2 # <===== Still 2 handlers (no duplicates)
<logging.FileHandler object at 0x7fd67a891c90>
<logging.StreamHandler object at 0x7fd67a862b10>

got logger instance in sub
2015-08-04 08:14:45,620 [sub.py] [<module>] [INFO] [10] utilizing sub_logger
exiting sub.py --------------------------------------------------

此外,如果您想限制要添加到记录器实例的处理程序的类型,您可以执行以下操作-

    print 'adding handlers- '
    # allows to add only one instance of file handler and stream handler
    if len(logger.handlers) > 0:
        print 'making sure we do not add duplicate handlers'
        for handler in logger.handlers:
              # add the handlers to the logger
              # makes sure no duplicate handlers are added

              if not isinstance(handler, logging.FileHandler) and not isinstance(handler, logging.StreamHandler):
                    logger.addHandler(fileHandler)
                    print 'added file handler'
                    logger.addHandler(consoleHandler)
                    print 'added stream handler'
    else:
        logger.addHandler(fileHandler)
        logger.addHandler(consoleHandler)
        print 'added handlers for the first time'

希望这会有所帮助!

编辑:

Unfortunately the same is not true for the handlers associated with the same instance of the logger. There can be duplicate handlers attached.

事实证明,上述说法并不完全正确。

假设我们在主模块中创建并配置了一个名为 'main_logger' 的记录器(它只是配置记录器,不返回任何内容)。

# get the logger instance
logger = logging.getLogger("main_logger")
# configuration follows
...

现在在子模块中,如果我们按照命名层次'main_logger.sub_module_logger'创建一个子记录器,我们不需要在子模块中配置它。只需按照命名层次结构创建记录器就足够了。

# get the logger instance
logger = logging.getLogger("main_logger.sub_module_logger")
# no configuration needed
# it inherits the configuration from the parent logger
...

它也不会添加重复的处理程序。

引用- Using logging in multiple modules

关于python logging 确保只添加一次处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6333916/

相关文章:

c# - 如何查看log4net日志路径?

azure - 如何调试 Azure 交换过程(有时会导致站点关闭)

linux - debian 上的 OpenJDK 崩溃日志

python - 将一系列数字更改为列表,python

python - Pandas :dt.dayofweek 显示不正确的值

python - 我收到 AttributeError : 'NoneType' object has no attribute 'find'

python - 在 Flask Admin 中用富文本编辑器替换 textarea

python - 使用 .format 使文本居中的方式正确吗?

c++ - 通过电线登录?

logging - 拦截来自 Ant 的 Commons 日志记录