我刚刚熟悉日志模块,不知道该如何正确地处理不同的级别。此flow chart指示没有任何低于设置级别的内容被传递给记录器对象,所以为什么会有人记录低于设置级别的任何内容?我应该创建多个日志文件吗?例如,在单个脚本中使用logging.basicConfig(filename='general_log.log',level=logging.WARNING)和logging.basicConfig(filename='detailed_log.log',level=logging.DEBUG)?
发布于 2016-02-06 05:58:19
多个日志记录级别用于不同的目的。例如,作为开发人员,当您开发一些新功能时,您可以将日志记录级别设置为DEBUG,这可以帮助您调试代码,并且当在生产环境中运行程序时,您可以将日志记录级别设置为INFO。所有调试级别的日志记录都将被静音。
您可以创建多个日志文件。例如,如果您想更容易地跟踪ERROR日志,可以将它们记录到一个单独的文件中,这样可以节省在常规日志文件中查找它们的时间。
发布于 2016-02-06 06:04:58
https://docs.python.org/2/library/logging.html#logging.basicConfig
This function does nothing if the root logger already has handlers configured for it.第二次调用logging.basicConfig可能什么也做不了。
要登录到多个文件,您需要配置多个处理程序:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(process)d %(thread)d %(name)s:%(lineno)s %(funcName)s() %(message)s'
},
},
'handlers': {
'file1': {
'level': 'WARNING',
'class': 'logging.FileHandler',
'filename': 'general_log.log',
'formatter': 'verbose',
},
'file2': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': 'detailed_log.log',
'formatter': 'verbose',
},
},
'root': {
'handlers': ['file1', 'file2'],
'level': 'DEBUG',
}
}
logging.config.dictConfig(LOGGING)https://stackoverflow.com/questions/35234089
复制相似问题