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

Feat: Rolling Appender Custom Rotation #2654

Open
wants to merge 23 commits into
base: master
Choose a base branch
from

Conversation

stegaBOB
Copy link

Motivation

Currently there are only 4 distinct rotations to choose from for the rolling appender: one minute, one hour, one day, and never. This PR makes it possible to specify custom durations such as 45 minutes, 6 hours, 5 years (???), or however often you desire your logs to be rotated. Closes #778.

Solution

I added an additional Custom variant to the RotationKind enum that stores a time::Duration, along with a new custom constructor on Rotation, which validates the duration length to be positive and less than 10 years. The 10 years is completely arbitrary and is mainly used to avoid potential time overflows with the OffsetDateTime in the next_date and round_date functions. This also means a 1ns duration is allowed. I think in practice this translates to something like 1 log per file, which may be useful somehow, but also feels a bit ridiculous. Would love some feedback on whether more realistic bounds would be better here.

I made the date_format for Custom be dynamic to avoid redundant precision.

The round_date implementation is probably the most important here and is equivalent to how the chrono crate does its DurationRound trait: https://github.com/chronotope/chrono/blob/38b19bbe4e21c402f81edfa2932a43831e679a35/src/round.rs#L214. The main differences are the lack of error handling (since we can't return a Result in this context) and using the euclidean remainder operation to handle negative current DateTimes (which shouldn't really be possible in the first place in our case) instead of their manual implementation it. Not having the error handling here is fine because all inputs should already be valid: the duration must be positive and the resulting DateTime should always be valid. I left comments explaining this.

Still to do:

  • Update the doc comments
  • Add a custom constructor method for RollingFileAppender

I held off on the last two things to make sure my implementation here is sufficient enough. If it looks okay, I'll be happy to finish that off!

@stegaBOB stegaBOB requested a review from a team as a code owner July 16, 2023 02:07
@stegaBOB
Copy link
Author

stegaBOB commented Nov 9, 2023

@hawkw Not sure who best to tag here, but I see that you commented back in 2020 on the original issue so I thought I'd try you! Would sorta prefer to get this merged in if possible while its still 2023 😅. If there's anything else I can do here to help get this in, just let me know!

@stegaBOB
Copy link
Author

stegaBOB commented Feb 6, 2024

Hi @davidbarsky! I saw you were active on some recent commits so thought I'd try tagging you for this. Would love to get this merged in at some point

Copy link
Contributor

@kaffarell kaffarell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, the unit-tests are also extensive :)

As you said, a few things are missing here, namely:

  • docs
  • public constructor in tracing-appender::rolling module.

Some small nits below:

tracing-appender/src/rolling.rs Show resolved Hide resolved
tracing-appender/src/rolling.rs Show resolved Hide resolved
@kaffarell
Copy link
Contributor

I think the change looks fine! Please add the missing docs and public method now, so that it only has to be reviewed once :)

@gibbz00
Copy link

gibbz00 commented Nov 7, 2024

Hi!

I would love to get this merged. Would it be ok for me to open a PR that takes this past the finish line?

@stegaBOB
Copy link
Author

stegaBOB commented Nov 7, 2024

Hi!

I would love to get this merged. Would it be ok for me to open a PR that takes this past the finish line?

Honestly I'd just be happy if a tracing maintainer responds at all to either of our PRs. It was a bit pathetic merging master into my branch for over a year.

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

Successfully merging this pull request may close these issues.

tracing-appender: Custom rotation duration
3 participants