简体   繁体   English

python django日志记录timerotatingfilehandler

[英]python django logging timerotatingfilehandler

I use the logging.handlers.TimedRotatingFileHandler to rotate the access log in my django project. 我使用logging.handlers.TimedRotatingFileHandler旋转django项目中的访问日志。 The setting of 'when' attribute is 'midnight', and the rotate works well most of the time, but sometimes it does not work as I want to, just like below: “时间”属性的设置为“午夜”,并且旋转在大多数情况下都能正常工作,但有时却无法如我所愿,如下所示:

-rw-rw-r-- 1 work work 5.5M Jun 28 23:59 sd.log.2014-06-28
-rw-rw-r-- 1 work work 5.1M Jun 27 23:59 sd.log.2014-06-27
-rw-rw-r-- 1 work work 5.2M Jun 26 23:59 sd.log.2014-06-26
-rw-rw-r-- 1 work work  146 Jun 26 00:00 sd.log.2014-06-25
-rw-rw-r-- 1 work work 5.1M Jun 24 23:59 sd.log.2014-06-24
-rw-rw-r-- 1 work work  123 Jun 24 00:00 sd.log.2014-06-23
-rw-rw-r-- 1 work work 5.2M Jun 22 23:59 sd.log.2014-06-22

Both the log of the 2014-06-23 and 2014-06-25 are rotated at 00:00 while others at 23:59 , and the file size is abnormal, too. 2014-06-232014-06-25的日志都在00:00旋转,而其他日志在23:59旋转,文件大小也异常。

PS: it writes to the log file any time when there is request call for my api. PS:只要有我的api的请求调用,它就会随时写入日志文件。

So, what's the reason cause the problem and how to solve it, has anyone met the problem, hope for your help, thanks a lot. 那么,是什么原因引起该问题以及如何解决它,有人解决了该问题,希望对您有所帮助,非常感谢。

You should check that your python process hasn't been interrupted. 您应该检查您的python进程没有被中断。 Originally TimedRotatingFileHandler was designed for longtime running processes. 最初, TimedRotatingFileHandler设计用于长时间运行的进程。 If your process was restarted many times during the day you would notice misbehaviours. 如果您的进程在一天内多次重新启动,您会注意到行为异常。

The explanation can be found at this thread . 可以在此线程中找到说明。

Please keep in mind the fact that Django's runserver by default restarts python process if project files were updated. 请记住,如果项目文件已更新,默认情况下Django的runserver重新启动python进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM