启用logging.propagate时的python 3.7记录问题

时间:2018-12-28 03:40:30

标签: python logging

我创建了记录器,并尝试添加处理程序。

import logging
from os.path import abspath, basename, dirname, join, splitext


def BuildLogger(sourcePath):  #, logPath=None):
    srcBasename = basename(sourcePath)
    logging.basicConfig(level=logging.DEBUG)  # Must have to see DEBUG/INFO at all
    logger = logging.getLogger(srcBasename)
    logger.propagate = False  # Must remove under py3.7, but keep for earlier versions to see logger message.

    # Avoid duplicated logs caused by duplicated handlers each time we create logger from a different module.
    if logger.hasHandlers():
        return logger

    # Console log is for end-users: no debug messages.
    consoleHanlder = logging.StreamHandler()
    consoleHanlder.setLevel(logging.INFO)
    consoleHanlder.setFormatter(logging.Formatter('%(levelname)s: %(_name)s: %(message)s'))
    logger.addHandler(consoleHanlder)

    return logger

if __name__ == '__main__':
    mylogger = BuildLogger(__file__)
    mylogger.info('hello')

但是,无论何时使用我的记录器登录,此代码都会引发KeyError

--- Logging error ---
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 1034, in emit
    msg = self.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 880, in format
    return fmt.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 622, in format
    s = self.formatMessage(record)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 591, in formatMessage
    return self._style.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 433, in format
    return self._fmt % record.__dict__
KeyError: '_name'
Call stack:
  File "/Users/kakyo/Desktop/0.Dev/playground/python/tkinter/logging_bug.py", line 25, in <module>
    mylogger.info('hello')
Message: 'hello'
Arguments: ()

我在BuildLogger()中进行了以下追踪:

logger = logging.getLogger(srcBasename)
logger.propagate = False 

我注释掉logger.propagate行之后,问题就消失了。

此问题似乎仅在Python 3.7中发生。我在Python 3.6中没有这个功能。实际上,我需要该行来显示记录器文件中的所有内容。

这是错误还是功能?

1 个答案:

答案 0 :(得分:2)

您正在访问格式化程序中的未定义属性,_name不存在,您必须使用name

logging.Formatter('%(levelname)s: %(name)s: %(message)s')

如果您仔细查看错误消息,它会告诉您:


--- Logging error ---
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 1034, in emit
    msg = self.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 880, in format
    return fmt.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 622, in format
    s = self.formatMessage(record)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 591, in formatMessage
    return self._style.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 433, in format
    return self._fmt % record.__dict__
KeyError: '_name'
Call stack:
  File "/Users/kakyo/Desktop/0.Dev/playground/python/tkinter/logging_bug.py", line 25, in 
    mylogger.info('hello')
Message: 'hello'
Arguments: ()