Python中的基本日志dictConfig [英] Basic logging dictConfig in Python
问题描述
NOTE I'm aware of this answer but that doesn't work for me, I am hoping for a complete, self-contained working example.
我正在尝试用Python(2.7)中的dictConfig
替换logging.basicConfig
I'm trying to replace logging.basicConfig
with dictConfig
in Python (2.7)
我的理解是basicConfig
只是设置了根记录程序:因此,我试图对dictConfig
做同样的事情.也就是说,使用处理程序设置根记录器,然后我的应用程序中的所有记录器都将在根记录器中传播.
My understanding is basicConfig
simply sets up the root logger: so I'm attempting to do the same with the dictConfig
. That is, set up the root logger with a handler, and then all the loggers in my application will propagate up the root logger.
以下代码段缺少什么?行为是创建了日志文件,但是没有输出. (我尝试过各种设置级别的组合,但似乎无济于事)
What's missing from the following snippet? The behaviour is the log file is created, but no output makes it. (I've tried various combos of setting levels, it doesn't appear to help)
import logging
log_dict = {
'loggers': {
'root': {
'handlers': ['file_handler']
}
},
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file_handler': {
'filename': 'c:/mylogfile.log',
'class': 'logging.FileHandler'
}
}
}
logging.config.dictConfig(log_dict)
logging.info('THIS IS A TEST')
logging.shutdown()
exit()
推荐答案
以下代码非常适合我:
import logging
import logging.config
log_dict = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
},
},
'handlers': {
'default': {
'level': 'INFO',
'formatter': 'standard',
'class': 'logging.StreamHandler',
},
'file_handler': {
'level': 'INFO',
'filename': '/tmp/mylogfile.log',
'class': 'logging.FileHandler',
'formatter': 'standard'
}
},
'loggers': {
'': {
'handlers': ['file_handler'],
'level': 'INFO',
'propagate': True
},
}
}
logging.config.dictConfig(log_dict)
logging.info("test")
事实上,它基于上述答案
And indeed, it's based on the answer mentioned above
这篇关于Python中的基本日志dictConfig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!