【问题标题】:unexpected keyword argument 'propagate'意外的关键字参数“传播”
【发布时间】:2021-04-24 10:50:01
【问题描述】:

我正在使用 python 的日志记录模块。对于配置,我正在尝试创建一个 yaml 格式的配置文件。但我收到错误

ValueError: Unable to configure handler 'file_handler': __init__() got an unexpected keyword argument 'propagate'

而我可以使用它logger.propagate = False。 我正在关注python logging yaml config,他们使用了相同的配置并能够解析它。

这就是我的 yaml 的样子

version: 1

formatters:
  simple:
    format: "%(asctime)s %(funcName)s %(levelname)s %(message)s"

handlers:
  console:
    class: logging.StreamHandler
    level: INFO
    formatter: simple

  file_handler:
    class: logging.FileHandler
    level: INFO
    filename: test_{}.log
    formatter: simple
    propagate: False

loggers:
  dev:
    handlers: [console, file_handler]
  test:
    handlers: [file_handler]
root:
  handlers: [file_handler]

这是我遇到的错误

 File "/usr/lib/python3.6/logging/config.py", line 565, in configure
    handler = self.configure_handler(handlers[name])
  File "/usr/lib/python3.6/logging/config.py", line 738, in configure_handler
    result = factory(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'propagate'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "logging_manager.py", line 34, in <module>
    LoggingManager()
  File "logging_manager.py", line 24, in __init__
    logging.config.dictConfig(config)
  File "/usr/lib/python3.6/logging/config.py", line 802, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib/python3.6/logging/config.py", line 573, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'file_handler': __init__() got an unexpected keyword argument 'propagate'

我在这里错过了什么?

编辑: 我正在使用环境

Python 3.6.9 (default, Oct  8 2020, 12:12:24)
[GCC 8.4.0] on linux

【问题讨论】:

  • Python 3.6.9 (default, Oct 8 2020, 12:12:24) [GCC 8.4.0] on linux

标签: python logging


【解决方案1】:

传播是记录器的属性,而不是处理程序的属性。您需要将其从您拥有的位置移除并将其放入记录器部分:

loggers:
  dev:
    handlers: [console, file_handler]
    propagate: False <---------------------- HERE for example
  test:
    handlers: [file_handler]

【讨论】:

  • 谢谢,它对我有用,但我分享的链接 python logging yaml config 他们正在配置中进行操作。它对他们有什么线索?
  • 这可能对他们不起作用,他们没有仔细检查粘贴到教程中的代码是否与他们机器上的代码相同。在处理程序上传播显然是非常错误的,因为处理程序不是层次结构的一部分,并且可以附加到许多记录器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 2016-11-12
  • 2020-09-12
  • 2017-06-29
相关资源
最近更新 更多