Skip to content

Commit 024d6ab

Browse files
zvkemphawkw
authored andcommitted
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.
1 parent f350c08 commit 024d6ab

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

tracing-appender/src/inner.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ impl InnerAppender {
6464
self.next_date = self.rotation.next_date(&now);
6565

6666
match create_writer(&self.log_directory, &filename) {
67-
Ok(writer) => self.writer = writer,
67+
Ok(writer) => {
68+
if let Err(err) = self.writer.flush() {
69+
eprintln!("Couldn't flush previous writer: {}", err);
70+
}
71+
self.writer = writer
72+
}
6873
Err(err) => eprintln!("Couldn't create writer for logs: {}", err),
6974
}
7075
}

0 commit comments

Comments
 (0)