[英]Python Logging config: Output trace to file, output log message to console
Given the following logging statement: 给定以下日志记录语句:
import logging
...
def ...
try:
...
except MyException:
...
logger.error("Parsing Error: %s"%(sys.exc_info()[0]),exc_info=True)
And the following json configuration: 以及以下json配置:
"handlers":{
"error_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "ERROR",
"formatter": "simple",
"filename": "../log/errors.log",
"maxBytes": 10485760,
"backupCount": 20,
"encoding": "utf8"
},
}
"loggers": {
"my_module": {
"level": "ERROR",
"handlers": ["console"],
"propagate": "no"
}
},
How do I modify this configuration such that the trace output does not output to the console, but rather to the error log? 如何修改此配置,以使跟踪输出不会输出到控制台,而是输出到错误日志?
And, at the same time: the log message string still outputs to the console? 并且,同时:日志消息字符串是否仍输出到控制台?
Try this: 尝试这个:
"handlers": {
"error_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "ERROR",
"formatter": "simple",
"filename": "../log/errors.log",
"maxBytes": 10485760,
"backupCount": 20,
"encoding": "utf8"
},
'console': {
'level': 'INFO',
'formatter': 'standard',
'class': 'logging.StreamHandler'
}
}
"loggers": {
"my_module": {
"level": "ERROR",
"handlers": ["console", "error_file_handler"],
"propagate": "no"
}
}
Change: 更改:
"handlers": ["console", "error_file_handler"]
: write log to both file and console "handlers": ["console", "error_file_handler"]
:将日志写入文件和控制台 "handlers": ["error_file_handler"]
: write log to file "handlers": ["error_file_handler"]
:将日志写入文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.