【问题标题】:Python Logging "KeyError" when loading fileConfig加载 fileConfig 时 Python 记录“KeyError”
【发布时间】:2012-02-13 07:46:59
【问题描述】:

我正在尝试在 python 中写出日志;我希望能够从文件加载配置,但调用后出现错误:

logging.config.fileConfig('logging.conf') 回溯(最近一次通话最后): 文件“/usr/lib64/python2.4/logging/config.py”,第 157 行,在 fileConfig log.addHandler(handlers[hand]) KeyError:'simpleHandler'

我的配置是:

  [loggers]
  keys=root

  [handlers]
  keys=simpleHandler

  [formatters]
  keys=simpleFormatter

  [logger_root]
  level=DEBUG
  handlers=simpleHandler

  [handler_simpleHandler]
  formatter=simpleFormatter
  class=handlers.RotatingFileHandler
  filename=/tmp/test.log
  maxBytes=31457280
  level=DEBUG

  [formatter_simpleFormatter]
  format=%(asctime)s %(levelname)s %(message)s
  datefmt=%Y/%m/%d %H:%M:%S

请帮我确定错误是什么,谢谢。

【问题讨论】:

  • 您使用的是 Python 2.4 版?那是很老了!你为什么用这个旧版本?

标签: python logging


【解决方案1】:

在你的文件处理程序中试试这个:

    [handler_simpleHandler]
    formatter=simpleFormatter
    class=handlers.RotatingFileHandler
    maxBytes=31457280
    level=DEBUG
    args=('/tmp/test.log',)

【讨论】:

  • 这成功了!从这里的另一篇文章中,我也得出了这个结论:args=('/tmp/test.log','a','maxBytes=31457280', 'backupCount=15')
【解决方案2】:

毫无疑问,密钥'simpleHandler' 在它应该存在的任何结构中都丢失了。

我建议将您的 handler_foo 部分放在您的 handlers 部分之前 - 看看是否有帮助。如果这没有帮助,请找到其他方法为您希望的处理程序命名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    相关资源
    最近更新 更多