[英]Including date in the log file name & creating log files of certain size while keeping last 3 log files in python with `logging.config.dictConfig()`
我正在使用配置設置 python 記錄器。 我希望日志文件包含日期(可能還有時間)。 我還希望在那個日期,每個日志文件只有特定的大小,並且只保留 3 個這樣的舊日志文件。
例如,我想獲取以下形式的日志文件:
mylog-2018-09-11.log
mylog-2018-09-11.log.1
mylog-2018-09-11.log.2
mylog-2018-09-11.log.3
我試着用RotatingFileHandler以下形式解釋的配置在這里:
config = {
#...
'handlers': {
'file': {
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'mplog.log',
'mode': 'w',
'formatter': 'detailed',
'maxBytes': 10000,
'backupCount': 3
},
},
#...
}
logging.config.dictConfig(config)
並生成如下日志文件:
mylog.log
mylog.log.1
mylog.log.2
mylog.log.3
但我無法理解,如何在日志文件名中獲取日期。 是否可以使用配置和一些處理程序來完成它,比如RotatingFileHandler
或TimeRotatingFileHandler
? 或者說我可以在dictConfig()
調用后以某種方式設置日志文件名以包含日期嗎?
您的示例顯示了一個RotatingFileHandler
,它根據大小而不是日期旋轉文件。 您需要使用TimedRotatingFileHandler
作為記錄在這里。 您可以按照與完成RotatingFileHandler
相同的方式對其進行配置。
更新:如果你想同時限制日期和大小,你將需要使用一個處理程序子類,它完全符合你的要求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.