Skip to content

Commit 47eb1ee

Browse files
committed
Check DateBased for rotation before logging, rather than after.
1 parent a47d3c7 commit 47eb1ee

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

src/log_impl.rs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -672,17 +672,22 @@ impl Log for DateBasedLogFile {
672672
fallback_on_error(record, |record| {
673673
let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
674674

675-
let just_reopened = match state.file_stream {
676-
Some(_) => false,
677-
None => {
678-
let suffix = self.config.compute_current_suffix();
679-
let file = self.config.open_current_log_file(&suffix)?;
680-
state.replace_file(suffix, Some(file));
681-
true
675+
// check if log needs to be rotated
676+
let new_suffix = self.config.compute_current_suffix();
677+
if state.file_stream.is_none() || state.current_suffix != new_suffix {
678+
let file_open_result = self.config.open_current_log_file(&new_suffix);
679+
match file_open_result {
680+
Ok(file) => {
681+
state.replace_file(new_suffix, Some(file));
682+
}
683+
Err(e) => {
684+
state.replace_file(new_suffix, None);
685+
return Err(e.into());
686+
}
682687
}
683-
};
688+
}
684689

685-
// just initialized writer above or returned.
690+
// either just initialized writer above, or already errored out.
686691
let writer = state.file_stream.as_mut().unwrap();
687692

688693
#[cfg(feature = "meta-logging-in-format")]
@@ -692,24 +697,6 @@ impl Log for DateBasedLogFile {
692697

693698
writer.flush()?;
694699

695-
if !just_reopened {
696-
// now check if log needs to be rotated
697-
let new_suffix = self.config.compute_current_suffix();
698-
if state.current_suffix != new_suffix {
699-
let file_open_result = self.config.open_current_log_file(&new_suffix);
700-
701-
match file_open_result {
702-
Ok(file) => {
703-
state.replace_file(new_suffix, Some(file));
704-
}
705-
Err(e) => {
706-
state.replace_file(new_suffix, None);
707-
return Err(e.into());
708-
}
709-
}
710-
}
711-
}
712-
713700
Ok(())
714701
});
715702
}

0 commit comments

Comments
 (0)