-
-
Notifications
You must be signed in to change notification settings - Fork 143
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
add a time roller #139
add a time roller #139
Conversation
301218d
to
3fa1f45
Compare
Hi @SSebo , I have some general comments:
Sorry for all the up front comments, but hopefully that helps, and thanks for the contribution! |
Thanks for your comment! I'll try to fix it.
|
|
One other comment would be that you should be able to do what you want using the fixed window roller. You'll just need to add an option that allows you to pass a file pattern containing a {t} If you need more info lmk, this would substantially reduce the amount of copy pasta. |
You mean we use two trigger, but the same |
|
@estk PTAL |
@estk Could you give me some advice ? If you think this PR is not acceptable, I can close it. That is ok. log4rs is a really good crate. |
Hi @SSebo , |
The first task I'd like to see here is removing the time based roller and using the fixedwindow roller in its place. |
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.
There are a few things I'd like to see addressed before merge.
- Time based roller: It isn't necessary, we need to use fixed_window_roller
- We cant make a public interface change to
trait Trigger
- There is a lot of noise in this PR due to code being moved around. The reorganization needs to be split out into a separate PR. In particular:
pattern/log_content
andpattern/log_path
should be moved back topattern/mod.rs
. To be clear, I think there is room for re-organization, but it should be in a separate cleanup PR.
@@ -188,6 +192,29 @@ impl<'a> Parser<'a> { | |||
} | |||
Piece::Text(&self.pattern[start..]) | |||
} | |||
|
|||
#[cfg(target_os = "windows")] | |||
fn text(&mut self, start: usize) -> Piece<'a> { |
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.
This is a public interface change, and I'd like it in a separate PR
#[cfg(target_os = "windows")] | ||
fn process_double_back_slash() -> Option<Piece<'a>> { | ||
Some(Piece::Text("\\")) | ||
} |
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.
Why was this change made?
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 found on Windows, when use set a path like C:\xxx\yyyy\log{}
, parser processed is C:\\xxx\\yyy\\log{}
. Currently parser not process \\
properly.
#[serde(deny_unknown_fields)] | ||
pub struct TimeTriggerConfig { | ||
#[serde(deserialize_with = "deserialize_unit")] | ||
unit: String, |
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.
This should deserialize into the type you want not a string. I think in this case its a TimeTrigger?
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.
Valid values of this field are 'day', 'hour' which specify how often the time trigger should be fired. Maybe unit
is confusing, if so could you give me some suggestion?
use std::cell::RefCell; | ||
|
||
#[cfg(test)] | ||
thread_local! { |
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.
Please add a comment on why this needs to be thread_local. A assume its some Send or Sync issue, why not use an Arc?
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.
When set_mock_time
invoked, RefCell can safely mutate on thread local variable.
We can also use unsafe
to make static mut
. I found the test case is not stable when use unsafe.
#[cfg(test)]
static mut MOCK_TIME_STR: Option<String> = None;
#[cfg(test)]
fn now_string(_fmt: &str) -> String {
unsafe {
if let Some(time_str) = &MOCK_TIME_STR {
time_str.to_string()
} else {
panic!("set mock now_string before use")
}
}
}
#[cfg(not(test))]
fn now_string(fmt: &str) -> String {
Local::now().format(fmt).to_string()
}
#[cfg(test)]
fn set_mock_time(time: &str) {
// MOCK_TIME_STR.with(|cell| *cell.borrow_mut() = Some(time.to_owned()));
unsafe {
MOCK_TIME_STR = Some(time.to_owned());
}
}
fn trigger() { | ||
set_mock_time("2020-03-07"); | ||
let trigger = TimeTrigger::new("%Y%m%d"); | ||
assert_eq!(false, trigger.trigger(Option::None).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.
You can use the tempfile crate to mock log files, I'd also like to see at least one test that is not using the mock time stuff.
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.
The time function is replaced under #[cfg(test)]
. If we use #[cfg(all(test, mock_time))]
on test mod, we need to add config settings when we run cargo test
. Any suggestions?
Additionally merging in master would be great, I've improved some CI things. Thanks @SSebo ! |
0ffb83a
to
6c49c48
Compare
@SSebo seems like there are still conflicts here, I'm going to close this for now. If its something you'd like to wrap up feel free to reopen. |
There is a need when you have some thing to log which should be keep for some days, but you can not estimate the size it will be (for example user online duration). If you use the size roller, the size may be small and log will be removed. A time roller would be nice to handle this thing.