Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【坑】logging的坑 #17

Open
Valuebai opened this issue Feb 7, 2020 · 7 comments
Open

【坑】logging的坑 #17

Valuebai opened this issue Feb 7, 2020 · 7 comments

Comments

@Valuebai
Copy link
Owner

Valuebai commented Feb 7, 2020

Python 日志模块 logging rotate 的坑儿:https://www.jianshu.com/p/25f70905ae9d

@Valuebai
Copy link
Owner Author

Valuebai commented Feb 9, 2020

使用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"

@Valuebai
Copy link
Owner Author

重写 TimedRotatingFileHandler 类,让logging支持多进程日志记录
chinapnr/fishbase#25

@Valuebai
Copy link
Owner Author

python logging日志模块以及多进程日志:https://www.jianshu.com/p/d615bf01e37b

@Valuebai
Copy link
Owner Author

TimedRotatingFileHandler 是 Python 提供的一个可以基于时间自动切分日志的 Handler 类,他继承自 BaseRotatingHandler -> logging.FileHandler

但是他有一个缺点就是没有办法支持多进程的日志切换,多进程进行日志切换的时候可能会因为重命名而丢失日志数据。

来看下他的实现(我默认大家已经知道了 FileHandler 的实现和 logging 模块的调用机制 如果还不清楚可以先去看下我前面那篇文章 https://www.cnblogs.com/piperck/p/9634133.html):

@Valuebai
Copy link
Owner Author

Python 日志logging模块初探及多线程踩坑(2) https://blog.csdn.net/qq_41603102/article/details/90200692

@Valuebai
Copy link
Owner Author

【我的处理】https://editor.csdn.net/md/?articleId=102668938

1 similar comment
@Valuebai
Copy link
Owner Author

【我的处理】https://editor.csdn.net/md/?articleId=102668938

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant