-
Notifications
You must be signed in to change notification settings - Fork 13
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
Feature/add duration rotation policy #74
Conversation
There was a problem hiding this 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.
/// 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, | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// 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
.
spdlog/examples/02_file.rs
Outdated
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(()) | ||
} |
There was a problem hiding this comment.
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.
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 | ||
)); | ||
} | ||
} |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, nice catch! 👍
The CI failure is fixed, you can rebase your branch to main branch once to sync the code. |
f18a4fb
to
6db9177
Compare
Thanks @SpriteOvO |
There was a problem hiding this 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.
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()) | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- Use a multiplication of 60 in
Duratuion::from_secs()
- Add dependency of
fastdata
(it's already imported as a dependency forfast_log
), and receive Duration::from_minute, Duration::from_hour, Duration::from_day.
Let me know what you prefer.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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 }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Period { duration: Duration }, | |
Period(Duration), |
Since it's just a single parameter I prefer to leave it unnamed.
spdlog/examples/02_file.rs
Outdated
duration: (chrono::Duration::hours(1) | ||
+ chrono::Duration::minutes(2) | ||
+ chrono::Duration::seconds(3)) | ||
.to_std() | ||
.unwrap(), |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
spdlog/examples/02_file.rs
Outdated
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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"); |
/// // Rotating periodically | ||
/// RotationPolicy::Period(std::time::Duration::from_secs(3 * 60 * 60 + 6)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// // 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)); |
… in example for users
c4c0416
to
73e43a6
Compare
There was a problem hiding this 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?
Thanks, |
@SpriteOvO @Lancern |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@lioriz Merged! Thanks, I will release the v0.4.0 ASAP. |
@SpriteOvO |
Adding
RotationPolicy::Duration
RotationPolicy
with the option to set the rotation time for any period of timeIssue: #73