使用“额外”时记录密钥错误

时间:2018-06-20 11:19:19

标签: python python-3.x logging

我有一个简单的日志记录设置:

extra = {'hostname': socket.gethostname(), 'ip': socket.gethostbyname(socket.gethostname()), 'user': getpass.getuser()}


def get_logger():
    logging.basicConfig(
        format='%(asctime)s # %(user)s # %(hostname)s # %(ip)s # %(message)s',
        datefmt='%m/%d/%Y %I:%M:%S')

    crawler_logger = logging.getLogger("crawler")
    crawler_logger.setLevel(logging.INFO)
    crawler_logger = logging.LoggerAdapter(crawler_logger, extra)
    return crawler_logger

我正在所有日志中使用此设置。一些例子:

logger.warning("Harmful URL # Potentially harmful url # %s" % url["url"])

logger.info("URL already exists: %s ", url_dict["url"])

logger.error("ERROR # ERROR while crawling url. %s" % e)

但是,每当我使用的任何库(例如,aiokafka)发生错误时,我的代码都会引发带有 KeyError:'user'的“ 记录错误”,即使“用户”键已存在,并在附加字典中得到了值。

错误的堆栈跟踪:

--- Logging error ---
Traceback (most recent call last):
  File "/usr/lib/python3.6/logging/__init__.py", line 992, in emit
    msg = self.format(record)
  File "/usr/lib/python3.6/logging/__init__.py", line 838, in format
    return fmt.format(record)
  File "/usr/lib/python3.6/logging/__init__.py", line 578, in format
    s = self.formatMessage(record)
  File "/usr/lib/python3.6/logging/__init__.py", line 547, in formatMessage
    return self._style.format(record)
  File "/usr/lib/python3.6/logging/__init__.py", line 391, in format
    return self._fmt % record.__dict__
KeyError: 'user'

我找不到任何解决方案,所以在这里问。

1 个答案:

答案 0 :(得分:1)

在我(不知不觉地)为所有记录器设置记录器格式之前,通过仅为我的自定义记录器设置记录器格式化程序来解决:

logging.basicConfig(
        format='%(asctime)s # %(user)s # %(hostname)s # %(ip)s # %(message)s',
        datefmt='%m/%d/%Y %I:%M:%S')

固定版本:

extra = {'hostname': socket.gethostname(), 'ip': socket.gethostbyname(socket.gethostname()), 'user': getpass.getuser()}


def get_logger():
    app_logger = logging.getLogger(__name__)
    handler = logging.StreamHandler()
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s # %(user)s # %(hostname)s # %(ip)s # %(message)s',
    datefmt='%d/%m/%Y %I:%M:%S')
    handler.setFormatter(formatter)
    app_logger.addHandler(handler)
    app_logger.setLevel(logging.INFO)
    app_logger = logging.LoggerAdapter(app_logger, extra)
    return app_logger