5

我正在尝试为 Python 配置一些日志记录。来自 http://docs.python.org/howto/logging.html 建议我们使用 YAML 配置文件——

version: 1
formatters:
  simple:
    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
  console:
    class: logging.StreamHandler
    level: DEBUG
    formatter: simple
    stream: ext://sys.stdout
loggers:
  simpleExample:
    level: DEBUG
    handlers: [console]
    propagate: no
root:
  level: DEBUG
  handlers: [console]

在我的代码中,我将其用作 -

import logging.config

logging.config.dictConfig('logging.config') 

但是,在运行它时,我得到一个错误

Traceback (most recent call last):
  File "TestLogger.py", line 62, in <module>
    Test5()  
  File "TestLogger.py", line 55, in Test5
    logging.config.dictConfig('logging.dict.2.config') 
  File "/usr/lib/python2.7/logging/config.py", line 776, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib/python2.7/logging/config.py", line 380, in __init__
    self.config = ConvertingDict(config)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

所以看来我以不正确的方式调用配置文件。只是找不到调用它的正确方法。请帮忙?非常感谢

4

1 回答 1

11

您需要加载并解析 YAML 文件:

import yaml
logging.config.dictConfig(yaml.load(open('logging.config', 'r')))
于 2012-08-21T15:59:25.250 回答