appender: explicitly flush previous BufWriter before dropping (#1604)

## 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.
This commit is contained in:
Zach Kemp 2021-10-01 10:11:50 -07:00 committed by Eliza Weisman
parent f350c08a26
commit 024d6abcf2

View File

@ -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),
}
}