kyn*_*kos 7 python configuration logging dictionary handler
这是关于Python 3.2(GNU/Linux x86_64)上的日志记录模块:是否可以使用字典配置设置自定义处理程序?这是我正在尝试的代码:
import logging
import logging.config
class CustomHandler(logging.StreamHandler):
pass
logconfig = {
'version': 1,
'handlers': {
'console': {
'class': 'CustomHandler',
}
},
'loggers': {
'custom': {
'handlers': ['console'],
}
}
}
logging.config.dictConfig(logconfig)
logger = logging.getLogger('custom')
logger.error('Error message')
Run Code Online (Sandbox Code Playgroud)
这当然不起作用.这是输出:
Traceback (most recent call last):
File "/usr/lib/python3.2/logging/config.py", line 390, in resolve
found = self.importer(used)
ImportError: No module named CustomHandler
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.2/logging/config.py", line 569, in configure
handler = self.configure_handler(handlers[name])
File "/usr/lib/python3.2/logging/config.py", line 698, in configure_handler
klass = self.resolve(config.pop('class'))
File "/usr/lib/python3.2/logging/config.py", line 403, in resolve
raise v
File "/usr/lib/python3.2/logging/config.py", line 390, in resolve
found = self.importer(used)
ValueError: Cannot resolve 'CustomHandler': No module named CustomHandler
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./prova.py", line 91, in <module>
logging.config.dictConfig(logconfig)
File "/usr/lib/python3.2/logging/config.py", line 777, in dictConfig
dictConfigClass(config).configure()
File "/usr/lib/python3.2/logging/config.py", line 574, in configure
'%r: %s' % (name, e))
ValueError: Unable to configure handler 'console': Cannot resolve 'CustomHandler': No module named CustomHandler
Run Code Online (Sandbox Code Playgroud)
还有的是,importer在这我真的不明白......任何想法来源的方法?
谢谢!
dap*_*wit 13
你需要告诉它在哪里找到这个CustomHandler班级.该字符串应包含可在其中找到的模块(以及可能的包).如果您直接运行此脚本,则可以使用__main__.CustomHandler.否则,请使用your_module.CustomHandler,替换your_module为包含该类的模块的名称.
| 归档时间: |
|
| 查看次数: |
4150 次 |
| 最近记录: |