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

Preserve a log file's modification time on rotation #20261

Open
spinitron opened this issue Oct 1, 2024 · 10 comments
Open

Preserve a log file's modification time on rotation #20261

spinitron opened this issue Oct 1, 2024 · 10 comments
Labels

Comments

@spinitron
Copy link

What steps will reproduce the problem?

Use Yii's FileTarget enough to cause log file rotation.

What is the expected result?

FS modification time of each log file indicates the date-time when that log file was last written.

What do you get instead?

It indicates when the file was last copied in a rotation.

It is likely enough to add one line to the FileTarget::rotateByCopy() function

        @touch($newFile, filemtime($rotateFile));
@samdark
Copy link
Member

samdark commented Oct 2, 2024

Is there a big difference? Why does it matter?

@rob006
Copy link
Contributor

rob006 commented Oct 2, 2024

Why do we even use copy() for older logs. Renaming active log file could be problematic, but it should be safe for older log files.

@spinitron
Copy link
Author

Is there a big difference?

Difference can be big. Say you have a log that rotates approx. every month and you keep 5. If when you go to look at the logs the rotation happened an hour ago, all 5 files have an mtime of one hour ago.

Why does it matter?

It matters because the correct mtime tells you when a file was last written. So if you want to look at logs surrounding a given timestamp, you can choose the file to look at from ls -l without needing to examine the files themselves.

@spinitron
Copy link
Author

Why do we even use copy() for older logs. Renaming active log file could be problematic, but it should be safe for older log files.

Good question. I guess it's because the code to copy the active file and rename the older ones would be more complex and nobody got around to writing it.

@samdark samdark added status:ready for adoption Feel free to implement this issue. and removed status:under discussion labels Oct 3, 2024
@spinitron
Copy link
Author

I can code this but I have no idea how to write the tests for it.

@samdark
Copy link
Member

samdark commented Oct 5, 2024

Yeah. Time-based tests that include file system are tricky.

@handcode
Copy link

Why do we even use copy()

JFTR: The option to use rotateByRename has been removed here: #19259

@samdark
Copy link
Member

samdark commented Dec 6, 2024

@spinitron is the issue still valid considering copy() was removed?

@rob006
Copy link
Contributor

rob006 commented Dec 6, 2024

@samdark copy() usage was not removed, it is enforced since #19259

@samdark
Copy link
Member

samdark commented Dec 7, 2024

Right!

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

No branches or pull requests

4 participants