From 024d6abcf245375ba1606dfeff6f87959501726b Mon Sep 17 00:00:00 2001 From: Zach Kemp Date: Fri, 1 Oct 2021 10:11:50 -0700 Subject: [PATCH] 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 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. --- tracing-appender/src/inner.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tracing-appender/src/inner.rs b/tracing-appender/src/inner.rs index f1e0fe0029..409918b159 100644 --- a/tracing-appender/src/inner.rs +++ b/tracing-appender/src/inner.rs @@ -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), } }