Skip to content

Commit

Permalink
appender: explicitly flush previous BufWriter before dropping (#1604)
Browse files Browse the repository at this point in the history
## Motivation

When a `RollingFileAppender` is refreshed, the previous `BufWriter` may
encounter and suppress errors in `drop`.

From https://doc.rust-lang.org/std/io/struct.BufWriter.html:

> It is critical to call flush before BufWriter<W> is dropped. Though
> dropping will attempt to flush the contents of the buffer, any errors
> that happen in the process of dropping will be ignored.

## Solution

Explicitly flush the previous buffer before dropping, printing any error
to stderr.
  • Loading branch information
zvkemp authored and hawkw committed Oct 1, 2021
1 parent f350c08 commit 024d6ab
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tracing-appender/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ impl InnerAppender {
self.next_date = self.rotation.next_date(&now);

match create_writer(&self.log_directory, &filename) {
Ok(writer) => self.writer = writer,
Ok(writer) => {
if let Err(err) = self.writer.flush() {
eprintln!("Couldn't flush previous writer: {}", err);
}
self.writer = writer
}
Err(err) => eprintln!("Couldn't create writer for logs: {}", err),
}
}
Expand Down

0 comments on commit 024d6ab

Please sign in to comment.