[英]Python Logging config: Output trace to file, output log message to console
給定以下日志記錄語句:
import logging
...
def ...
try:
...
except MyException:
...
logger.error("Parsing Error: %s"%(sys.exc_info()[0]),exc_info=True)
以及以下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"
}
},
如何修改此配置,以使跟蹤輸出不會輸出到控制台,而是輸出到錯誤日志?
並且,同時:日志消息字符串是否仍輸出到控制台?
嘗試這個:
"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"
}
}
更改:
"handlers": ["console", "error_file_handler"]
:將日志寫入文件和控制台 "handlers": ["error_file_handler"]
:將日志寫入文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.