我需要在 json 中准备一个日志配置文件,该文件按时间、大小和压缩 de 文件旋转,为我的应用程序中的所有模块旋转(我现在卡住了)。我想使用一个 json 配置文件来完成它,这是我当前的文件,但是这个配置只按时间轮换:
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"simple": {
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
}
},
"handlers": {
"my_rotate": {
"level": "DEBUG",
"class": "logging.handlers.TimedRotatingFileHandler",
"formatter": "simple",
"when": "D",
"interval": 1,
"backupCount": 5,
"filename": "/var/log/test.log",
"encoding": "utf8"
}
},
"loggers": {
"my_module": {
"level": "DEBUG",
"handlers": ["my_rotate"]
}
},
"root": {
"level": "DEBUG",
"handlers": ["my_rotate"],
"propagate": false
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在处理程序部分(logging.handlers.RotateFileHandler)中添加其他处理程序以按大小旋转结果日志文件重复所有消息。
我的主程序使用以下方法获取记录器属性:
config_json_file = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "logging.json"))
logging_data = json.load(config_json_file) …Run Code Online (Sandbox Code Playgroud)