Skip to content

Commit

Permalink
appender: prune old files at startup
Browse files Browse the repository at this point in the history
The prune_old_logs function only gets called when we rollover the
appender. The problem is that at startup we create an initial log file
without rolling over, if e.g., the executable then exits before rolling
over for the first time, we never call prune_old_logs.

E.g.: If we have a tracing-appender that rolls over every minute and we
have a max_files parameter of 2. Running the program (which prints a
single log line and exits) every minute, will result in a new log file
everytime without deleting the old ones.

Fixes: tokio-rs#2937
  • Loading branch information
kaffarell committed May 10, 2024
1 parent 908cc43 commit 7ae3479
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tracing-appender/src/rolling.rs
Expand Up @@ -543,6 +543,11 @@ impl Inner {
rotation,
max_files,
};

if let Some(max_files) = max_files {
inner.prune_old_logs(max_files);
}

let filename = inner.join_date(&now);
let writer = RwLock::new(create_writer(inner.log_directory.as_ref(), &filename)?);
Ok((inner, writer))
Expand Down

0 comments on commit 7ae3479

Please sign in to comment.