我正在尝试为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]
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我用它作为 -
import logging.config
logging.config.dictConfig('logging.config')
Run Code Online (Sandbox Code Playgroud)
但是,在运行它时,我收到一个错误
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
Run Code Online (Sandbox Code Playgroud)
所以我似乎以不正确的方式调用配置文件.只是找不到调用它的正确方法.请帮忙?非常感谢
您需要加载并解析YAML文件:
import yaml
logging.config.dictConfig(yaml.load(open('logging.config', 'r')))
Run Code Online (Sandbox Code Playgroud)