Skip to content

Commit

Permalink
chore: prune when startup
Browse files Browse the repository at this point in the history
  • Loading branch information
flisky committed Feb 9, 2023
1 parent d4f7c84 commit 8ff65b8
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tracing-appender/src/rolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ impl Inner {
max_files,
};
let filename = inner.join_date(&now);
inner.prune_old_logs(&filename);
let writer = RwLock::new(create_writer(inner.log_directory.as_ref(), &filename)?);
Ok((inner, writer))
}
Expand All @@ -584,7 +585,7 @@ impl Inner {
}
}

fn prune_old_logs(&self) {
fn prune_old_logs(&self, new_filename: &str) {
let log_prefix = self
.log_filename_prefix
.as_ref()
Expand All @@ -608,6 +609,10 @@ impl Inner {
let filename = entry.file_name();
// if the filename is not a UTF-8 string, skip it.
let filename = filename.to_str()?;
// exclude current log file
if filename == new_filename {
return None;
}
// strip compressed file extension
let (need_compress, mut filename) = compress_ext
.and_then(|compress_ext| {
Expand Down Expand Up @@ -709,9 +714,9 @@ impl Inner {
if let Err(err) = file.flush() {
eprintln!("Couldn't flush previous writer: {}", err);
}
self.prune_old_logs();

let filename = self.join_date(&now);
self.prune_old_logs(&filename);
match create_writer(&self.log_directory, &filename) {
Ok(new_file) => *file = new_file,
Err(err) => eprintln!("Couldn't create writer for logs: {}", err),
Expand Down

0 comments on commit 8ff65b8

Please sign in to comment.