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

Feature/add duration rotation policy #74

Merged
merged 20 commits into from
Sep 30, 2024

Conversation

lioriz
Copy link
Contributor

@lioriz lioriz commented Sep 18, 2024

Adding RotationPolicy::Duration

  • Rotating after defined duration (hours, minutes, seconds)
  • Adding flexible RotationPolicy with the option to set the rotation time for any period of time
  • Adding unit tests for the new policy

Issue: #73

Copy link
Owner

@SpriteOvO SpriteOvO left a comment

Choose a reason for hiding this comment

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

👍 Thanks for your work on this feature! The added tests are pretty good. I just commented some change suggestions, please have a look.

By the way, the CI is failing because of the other part of the code, I'll fix it in the main branch so you don't have to worry about it for now.

Comment on lines 69 to 77
/// Rotating to a new log file after given duration (greater then {0, 0, 0}) is passed.
Duration {
/// Hours to the next rotation.. Range: [0, u32::MAX].
hours: u32,
/// Minutes to the next rotation. Range: [0, 59].
minutes: u32,
/// Seconds to the next rotation. Range: [0, 59].
seconds: u32,
},
Copy link
Owner

@SpriteOvO SpriteOvO Sep 18, 2024

Choose a reason for hiding this comment

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

Suggested change
/// Rotating to a new log file after given duration (greater then {0, 0, 0}) is passed.
Duration {
/// Hours to the next rotation.. Range: [0, u32::MAX].
hours: u32,
/// Minutes to the next rotation. Range: [0, 59].
minutes: u32,
/// Seconds to the next rotation. Range: [0, 59].
seconds: u32,
},
/// Rotating to a new log file after a given period has elapsed.
Period(
/// Period duration. Must be longer than 1 minute.
Duration,
),

That's a little different than what I had in mind. std::time::Duration should be more appropriate here as the parameter.

Considering a user wants to rotate every week, in the current way they have to write Duration { hours: 7 * 24, minutes: 0, seconds: 0 }, if we use Duration, just a Duration::from_weeks(1) will work. Something like 1.5 hours can also be expressed as Duration::from_hours(1) + Duration::from_mins(30).

And I prefer the name "Period" because we have already use the term in Logger::set_flush_period.

info!("this log will be written to the file `rotating_duration.log`, and the file will be rotated every hour, 2 minutes, and 3 seconds");

Ok(())
}
Copy link
Owner

Choose a reason for hiding this comment

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

I like the more detailed examples. Just a nitpick - the file should ends with a newline.

Comment on lines 292 to 305
Self::Duration {
hours,
minutes,
seconds,
} => {
if *hours == 0 && *minutes == 0 && *seconds == 0
|| *minutes > 59
|| *seconds > 59 {
return Err(format!(
"policy 'duration' expect `(hours, minutes, seconds)` to be ([0, u32::MAX], [0, 59], [0, 59]) and greater then (0, 0, 0) but got ({}, {}, {})",
*hours, *minutes, *seconds
));
}
}
Copy link
Owner

Choose a reason for hiding this comment

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

If we use Duration as the parameter, it has already ensured the value correctness, thanks to which we don't need to check it again in our code.

But I want to return an error here if duration < Duration::mins(1), as I commented #73 (reply in thread) before, a rotating policy of less than 1 minute doesn't make much sense.

};

if rotation_time < now {
if rotation_time <= now {
Copy link
Owner

Choose a reason for hiding this comment

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

Makes sense, nice catch! 👍

@SpriteOvO
Copy link
Owner

The CI failure is fixed, you can rebase your branch to main branch once to sync the code.

@lioriz lioriz force-pushed the feature/add-duration-rotation-policy branch from f18a4fb to 6db9177 Compare September 20, 2024 12:06
@lioriz1
Copy link

lioriz1 commented Sep 20, 2024

👍 Thanks for your work on this feature! The added tests are pretty good. I just commented some change suggestions, please have a look.

Thanks @SpriteOvO
I updated the PR according to the review, it makes sense to use Duration instead [hour, minute, second], as it looks better and uses a standard time format.

Copy link
Owner

@SpriteOvO SpriteOvO left a comment

Choose a reason for hiding this comment

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

Most looks good to me now! There are just 2 tiny code issues, and some formatting issues that should be fixed by running cargo fmt --all once.

Comment on lines 76 to 91
fn minutes(minutes: u64) ->Duration {
chrono::Duration::minutes(minutes as i64).to_std().expect(format!("Failed to create Duration::minutes({}", minutes).as_str())
}

fn hours(hours: u64) -> Duration {
chrono::Duration::hours(hours as i64).to_std().expect(format!("Failed to create Duration::hours({}", hours).as_str())
}

fn days(days: u64) -> Duration {
chrono::Duration::days(days as i64).to_std().expect(format!("Failed to create Duration::days({}", days).as_str())
}

git
fn weeks(weeks: u64) -> Duration {
chrono::Duration::weeks(weeks as i64).to_std().expect(format!("Failed to create Duration::weeks({}", weeks).as_str())
}
Copy link
Owner

@SpriteOvO SpriteOvO Sep 20, 2024

Choose a reason for hiding this comment

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

It doesn't look like chrono::Duration needs to be involved here, std::time::Duration should be enough.

There is also an accidentally added "git" line that should be removed.

Copy link

Choose a reason for hiding this comment

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

std::time::Duration doesn't have minutes, hours, and days out of the box, so I used chrono for readability.
I see two options for change:

Let me know what you prefer.

Copy link
Owner

@SpriteOvO SpriteOvO Sep 21, 2024

Choose a reason for hiding this comment

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

I think multiplication of 60 in Duratuion::from_secs() is accepted, as long as we wrap each unit as a constant or function for readability.

Copy link

Choose a reason for hiding this comment

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

So you want to remove those wrapper functions and just use const val with Duration::from_secs or use those wrapper functions with the const val and Duration::from_secs?

Copy link
Owner

@SpriteOvO SpriteOvO Sep 21, 2024

Choose a reason for hiding this comment

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

I think const val is better, because there is no function and we don't need to worry about whether they can be inlined at all.

@@ -102,6 +127,7 @@ struct RotatorTimePoint {
enum TimePoint {
Daily { hour: u32, minute: u32 },
Hourly,
Period { duration: Duration },
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
Period { duration: Duration },
Period(Duration),

Since it's just a single parameter I prefer to leave it unnamed.

Comment on lines 88 to 92
duration: (chrono::Duration::hours(1)
+ chrono::Duration::minutes(2)
+ chrono::Duration::seconds(3))
.to_std()
.unwrap(),
Copy link
Owner

Choose a reason for hiding this comment

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

spdlog-rs doesn't expose chrono to users, all chrono stuff are hidden under the hood, therefore all API accepts std Duration instead of chrono Duration.

Thus, chrono Duration should not appear in the examples as well. Let's change it to just using std Duration.

Copy link
Owner

@SpriteOvO SpriteOvO left a comment

Choose a reason for hiding this comment

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

Just nitpicks, use more reasonable values in example for users.

Comment on lines 84 to 95
let file_sink = Arc::new(
RotatingFileSink::builder()
.base_path(path)
.rotation_policy(RotationPolicy::Period(Duration::from_secs(
60 * 60 * 2 + 60 * 4 + 5,
)))
.build()?,
);
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
spdlog::set_default_logger(new_logger);

info!("this log will be written to the file `rotating_period.log`, and the file will be rotated every hour, 2 minutes, and 3 seconds");
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
let file_sink = Arc::new(
RotatingFileSink::builder()
.base_path(path)
.rotation_policy(RotationPolicy::Period(Duration::from_secs(
60 * 60 * 2 + 60 * 4 + 5,
)))
.build()?,
);
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
spdlog::set_default_logger(new_logger);
info!("this log will be written to the file `rotating_period.log`, and the file will be rotated every hour, 2 minutes, and 3 seconds");
let file_sink = Arc::new(
RotatingFileSink::builder()
.base_path(path)
.rotation_policy(RotationPolicy::Period(Duration::from_secs(
60 * 90, // 90 minutes
)))
.build()?,
);
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
spdlog::set_default_logger(new_logger);
info!("this log will be written to the file `rotating_period.log`, and the file will be rotated every 1.5 hours");

Comment on lines 49 to 50
/// // Rotating periodically
/// RotationPolicy::Period(std::time::Duration::from_secs(3 * 60 * 60 + 6));
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
/// // Rotating periodically
/// RotationPolicy::Period(std::time::Duration::from_secs(3 * 60 * 60 + 6));
/// // Rotating every 6 hour.
/// # use std::time::Duration;
/// RotationPolicy::Period(Duration::from_secs(6 * 60 * 60));

@lioriz lioriz force-pushed the feature/add-duration-rotation-policy branch from c4c0416 to 73e43a6 Compare September 21, 2024 15:29
Copy link
Owner

@SpriteOvO SpriteOvO left a comment

Choose a reason for hiding this comment

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

LGTM now! Thank you for continually improving this PR.

@Lancern Would you like to take a look at this PR?

@lioriz1
Copy link

lioriz1 commented Sep 22, 2024

LGTM now! Thank you for continually improving this PR.

@Lancern Would you like to take a look at this PR?

Thanks,
Waiting for this one in my project

@Lancern Lancern self-requested a review September 22, 2024 06:33
@lioriz
Copy link
Contributor Author

lioriz commented Sep 30, 2024

@SpriteOvO @Lancern
Hi,
Do you need more work on this from my side for approval?

Copy link
Collaborator

@Lancern Lancern left a comment

Choose a reason for hiding this comment

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

LGTM!

@SpriteOvO SpriteOvO merged commit 16e259e into SpriteOvO:main Sep 30, 2024
33 checks passed
@SpriteOvO
Copy link
Owner

@lioriz Merged! Thanks, I will release the v0.4.0 ASAP.

@lioriz
Copy link
Contributor Author

lioriz commented Oct 1, 2024

@lioriz Merged! Thanks, I will release the v0.4.0 ASAP.

@SpriteOvO
Thank you for accepting my proposal and merging the PR.
Waiting for this feature.

@lioriz lioriz deleted the feature/add-duration-rotation-policy branch October 1, 2024 07:37
@SpriteOvO
Copy link
Owner

@lioriz Hi, v0.4.0 has been released. Let me know if it works for you :)

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.

4 participants