简体   繁体   中英

python django logging timerotatingfilehandler

I use the logging.handlers.TimedRotatingFileHandler to rotate the access log in my django project. 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.

PS: it writes to the log file any time when there is request call for my 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. Originally TimedRotatingFileHandler was designed for longtime running processes. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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