This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: TimedRotatingFileHandler rotating on use not time
Type: Stage: patch review
Components: Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: markhallett, martonivan, steven.daprano, vinay.sajip
Priority: normal Keywords:

Created on 2020-05-01 15:02 by markhallett, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
logging_config.ymal markhallett, 2020-05-01 15:02
app.py markhallett, 2020-05-02 10:03 Eg code to use the ymal file
config.json vinay.sajip, 2021-08-01 06:15 JSON version of test config
log_40469.py vinay.sajip, 2021-08-01 06:19 Modified test script
log_40469_new_instance.py martonivan, 2021-08-10 09:00
log_40469_single.py martonivan, 2021-08-10 09:00
Pull Requests
URL Status Linked Edit
PR 24660 open martonivan, 2021-02-26 11:37
Messages (11)
msg367849 - (view) Author: Mark Hallett (markhallett) Date: 2020-05-01 15:02
Using the attached confing ymal file, the log file only rolls if the log file is not written to for over a minute, logging regulary (eg every 30s)  prevents the correct rolling of the log.
msg367922 - (view) Author: Mark Hallett (markhallett) Date: 2020-05-02 10:03
Eg code to run is now attached
msg398684 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2021-08-01 06:19
With this slightly modified test script and the JSON configuration based on the YAML, I got the following results after running for a while:

$ more info.log*
::::::::::::::
info.log
::::::::::::::
2021-08-01 07:13:45 INFO Message no. 9
2021-08-01 07:14:15 INFO Message no. 10
::::::::::::::
info.log.2021-08-01_07-09
::::::::::::::
2021-08-01 07:09:45 INFO Message no. 1
2021-08-01 07:10:15 INFO Message no. 2
::::::::::::::
info.log.2021-08-01_07-10
::::::::::::::
2021-08-01 07:10:45 INFO Message no. 3
2021-08-01 07:11:15 INFO Message no. 4
::::::::::::::
info.log.2021-08-01_07-11
::::::::::::::
2021-08-01 07:11:45 INFO Message no. 5
2021-08-01 07:12:15 INFO Message no. 6
::::::::::::::
info.log.2021-08-01_07-12
::::::::::::::
2021-08-01 07:12:45 INFO Message no. 7
2021-08-01 07:13:15 INFO Message no. 8

The test script logs every 30 seconds, so I don't see what the problem is. Can you describe it more clearly?
msg399306 - (view) Author: Ivan Marton (martonivan) * Date: 2021-08-10 09:00
The assumed behaviour of TimedRotatingFileHandler is to rotate log files older than configured. Even when the script is executed multiple times.

self.rolloverAt (the variable that defines when the rollover should be performed) is set after each rollover and right after initializing the file handler instance.

If the instance is initialized once (like in your script) and a rollover is performed without having the object destroyed, the logger works fine, the rollover is done and the next round is scheduled properly.

The case is not so lucky if the script runs multiple time or the logger itself is initialized multiple times during one execution. In this case, since the MTIME is read each time when the TimedRotatingFileHandler's init is called, and the file is modified (by having a new line added to the end of the file). The next execution will read the new MTIME and will never perform any rollover.

I've slightly modified your example script to demonstrate these use-cases.

Example 1: Log a single line with the script, but execute it multiple times with delays between the execution!
In bash: for i in {1..101}; do python log_40469_single.py $i; sleep 30; done

Example 2: Log multiple lines, but reinitiate the logger object between the events!
See log_40469_new_instance.py
msg399481 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2021-08-12 20:27
> The assumed behaviour of TimedRotatingFileHandler is to rotate log files older than configured. Even when the script is executed multiple times.

Ah, but is it? The purpose of TimedRotatingFileHandler is to rotate files based on time intervals, and is normally assumed to be for long-running processes (where the rotation allows one to focus on logs output during a period of interest). What's the practical, real-world use-case for the example scripts you added?
msg399496 - (view) Author: Ivan Marton (martonivan) * Date: 2021-08-13 07:09
Well, there is nothing in the documentation that would say the logrotation can be only performed by continously running scripts, thats why. :)

There are scripts or even daemons written in Python that need to be restarted from time to time. If the logrotate period is greater than restart period the log files will never be rotated, however they could be.

We were looking for something that works like the logrotate do, but without involving any external logic, but the one written in python. And TimedLogrotateHandler appears to be one of those, unless it checks the MTIME of the files instead of the CTIME.

What is the logic behind checking the MTIME instead of CTIME when one is calculating the age of a file when tries to calculate whether it has to be rotated or not? If the only purpose of this initial rollover calculation would be to start a predefined long period (as you suggest), now() (time of execution) should used instead and no file attribute should be involved. From the code I have to assume that the original author also tried to prepare for the cases where the execution is interrupted and/or restarted and (s)he wanted the TimedLogrotateHandler to be resumed where it was during the previous execution. Dnn't you agree?
msg399499 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2021-08-13 08:06
> Well, there is nothing in the documentation that would say the logrotation can be only performed by continously running scripts, thats why. :)

Well, what's the need to rotate based on time when you just run scripts sporadically or they're very short-lived? Not trying to argue, just trying to understand.

> There are scripts or even daemons written in Python that need to be restarted from time to time. If the logrotate period is greater than restart period the log files will never be rotated, however they could be.

Yes, but generally daemons are long-lived relative to the rotation interval, aren't they? "From time to time" certainly implies that to me.

> What is the logic behind checking the MTIME instead of CTIME when one is calculating the age of a file when tries to calculate whether it has to be rotated or not?

I can't remember, that code is over 12 years old and that specific decision wasn't documented :-(

> From the code I have to assume that the original author also tried to prepare for the cases where the execution is interrupted and/or restarted and (s)he wanted the TimedLogrotateHandler to be resumed where it was during the previous execution. Dnn't you agree?

I take it you mean TimedRotatingFileHandler. Certainly it assumes that the script might be restarted, which is why it opens the file in append mode.

If the computation using MTIME were changed to use CTIME, that would be a behaviour change, which could conceivably break someone's code that relied on current behaviour (unlikely, but you just never know). I've no other objection to using CTIME rather than MTIME - perhaps I'm just being hyper-cautious about breakage?
msg399500 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2021-08-13 08:11
I just checked - the relevant code (using MTIME) was added in 2010, as a fix for bpo-8117 - around the time that Python 2.6 was released.
msg399506 - (view) Author: Ivan Marton (martonivan) * Date: 2021-08-13 08:42
> Well, what's the need to rotate based on time when you just run scripts sporadically or they're very short-lived? Not trying to argue, just trying to understand.
> Yes, but generally daemons are long-lived relative to the rotation interval, aren't they? "From time to time" certainly implies that to me.

In one of our use-cases, where I faced with this bug/behaviour of TimedRotatingFileHandler, we are using it in a daemon for daily logrotate. The aim is to have the logs separated day by day, just like the majority of our other components running on a linux. We've just realized that some days are missing from the series of logs and the investigation led to the service restart and the MTIME checking that we are talking about here. When a logger is configured to rotate daily one just simply assume that it will rotate daily regardless the daemon restart. (As en extreme(?) example, when a the system - and the daemon of course - is restarted each day - or just never running for a whole day - and btw it logs often, the log file won't be ever rotated and can inrease over every limits.)

I've quickly googled around and found someone who execute some logic from cron and expects the loghandler to rotate his files, that wouldn't happened for the same reason:
https://stackoverflow.com/questions/30569164/logging-handler-timedrotatingfilehandler-never-rotates-the-log

> I just checked - the relevant code (using MTIME) was added in 2010, as a fix for bpo-8117 - around the time that Python 2.6 was released.
As I understand bpo-8117, the goal was to do the rollover even after script restarts. (To be frank, only an extreme case was mentioned in the issue opener entry, when the rollover period had been reached when the script wasn't running.) I believe for this purpose MTIME wasn't the best choice, but using CTIME instead would make the script do what is expected to.

> I take it you mean TimedRotatingFileHandler. Certainly it assumes that the script might be restarted, which is why it opens the file in append mode.

> If the computation using MTIME were changed to use CTIME, that would be a behaviour change, which could conceivably break someone's code that relied on current behaviour (unlikely, but you just never know). I've no other objection to using CTIME rather than MTIME - perhaps I'm just being hyper-cautious about breakage?

You are far better and more experienced in maintaining widely used opensource codebases so I wouldn't argue with you, but... :)

- In case of scripts that create the log file for themselves the change would have no effect.
- In case of scripts that are using already existing files for logging (by opening them for append) could change the behaviour, but just slighly. The first rollover would happen before adding the first new log line if the file age reached the configured time, and not when the age of last log line written to the file before having the script started reached the same value. No further rotation would be effected.

I _believe_ no one expects the current initial behaviour in case of resumed log files, and for sure, the documentation doesn't cover this part so I don't know how to proceed.

I _believe_ (again) that changing the behaviour wouldn't break the script outside, but rather would help them working the way how the developers originally assumed they did.
msg399531 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2021-08-13 12:12
> You are far better and more experienced in maintaining widely used opensource codebases so I wouldn't argue with you, but... :)

Well, if a change introduces a problem, it will be my lookout, won't it? Just as the original patch for bpo-8117 is proving to be :-)

OK, how about this? We update the PR to add a new use_ctime=False keyword argument to TimedRotatingHandler.__init__(). If nothing else changes, the behaviour is as it is currently. If use_ctime=True is passed in the instantiation, then the constructor will use CTIME rather than MTIME. Will that work for you? I'd be OK with that approach.
msg416458 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2022-03-31 20:48
See this thread on Discuss:

https://discuss.python.org/t/logging-timedrotatingfilehandler-never-rotates-in-certain-cases/14747/1
History
Date User Action Args
2022-04-11 14:59:30adminsetgithub: 84649
2022-03-31 20:48:10steven.dapranosetnosy: + steven.daprano
messages: + msg416458
2021-08-13 12:12:02vinay.sajipsetmessages: + msg399531
2021-08-13 08:42:17martonivansetmessages: + msg399506
2021-08-13 08:11:38vinay.sajipsetmessages: + msg399500
2021-08-13 08:06:40vinay.sajipsetmessages: + msg399499
2021-08-13 07:09:43martonivansetmessages: + msg399496
2021-08-12 20:27:28vinay.sajipsetmessages: + msg399481
2021-08-10 09:00:20martonivansetfiles: + log_40469_single.py
2021-08-10 09:00:11martonivansetfiles: + log_40469_new_instance.py

messages: + msg399306
2021-08-01 06:19:21vinay.sajipsetkeywords: - patch
files: + log_40469.py
messages: + msg398684
2021-08-01 06:15:13vinay.sajipsetfiles: + config.json
2021-02-28 20:32:16rhettingersetnosy: + vinay.sajip
2021-02-26 11:37:42martonivansetkeywords: + patch
nosy: + martonivan

pull_requests: + pull_request23445
stage: patch review
2020-05-02 10:03:02markhallettsetfiles: + app.py

messages: + msg367922
2020-05-01 15:02:02markhallettcreate