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 · 6 comments
Open

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

spinitron opened this issue Oct 1, 2024 · 6 comments
Labels
status:ready for adoption Feel free to implement this issue.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for adoption Feel free to implement this issue.
Projects
None yet
Development

No branches or pull requests

3 participants