Open Valuebai opened 4 years ago
使用TimedRotatingFileHandler 必须加的:
默认 python 库中的 logging.handlers.TimedRotatingFileHandler 会在 logger 初始化阶段不生成 suffix,这样一旦程序重启就会导致上次启动的日志被覆盖。
解决办法: 在初始化 TimedRotatingFileHandler 之后,设置 suffix
# 建立一个循环文件handler来把日志记录在文件里
file_handler = TimedRotatingFileHandler(
filename=self.logs_dir + r'/log.log', # 定义日志的存储
when="S", # 按照日期进行切分when = D: 表示按天进行切分,or self.when == 'MIDNIGHT'
interval=1, # interval = 1: 每天都切分。 比如interval = 2就表示两天切分一下。
backupCount=30, # 最多存放日志的数量
encoding="UTF-8", # 使用UTF - 8的编码来写日志
delay=False,
# utc = True: 使用UTC + 0的时间来记录 (一般docker镜像默认也是UTC + 0)
)
file_handler.suffix = "%Y-%m-%d_%H-%M-%S.log"
用D记录日志的话,得用-->>file_handler.suffix = "%Y-%m-%.log"
重写 TimedRotatingFileHandler 类,让logging支持多进程日志记录 https://github.com/chinapnr/fishbase/issues/25
python logging日志模块以及多进程日志:https://www.jianshu.com/p/d615bf01e37b
TimedRotatingFileHandler 是 Python 提供的一个可以基于时间自动切分日志的 Handler 类,他继承自 BaseRotatingHandler -> logging.FileHandler
但是他有一个缺点就是没有办法支持多进程的日志切换,多进程进行日志切换的时候可能会因为重命名而丢失日志数据。
来看下他的实现(我默认大家已经知道了 FileHandler 的实现和 logging 模块的调用机制 如果还不清楚可以先去看下我前面那篇文章 https://www.cnblogs.com/piperck/p/9634133.html):
Python 日志logging模块初探及多线程踩坑(2) https://blog.csdn.net/qq_41603102/article/details/90200692
Python 日志模块 logging rotate 的坑儿:https://www.jianshu.com/p/25f70905ae9d