I tried this code in my project, and it didn't work well. Only last message is saved to file. At first I thought that this is bug in tracing. So I wanted to report it to tracing. Then I discovered that .with_writer(File::create("/tmp/log.json").unwrap()) works well (i. e. saves all messages) and .with_writer(|| File::create("/tmp/log.json").unwrap()) doesn't (saves last message only).
It seems if we pass closure to with_writer, then the closure is called every time tracing wants to write something. So the file is truncated every time. So, please, write .with_writer(File::create("/tmp/log.json").unwrap())
It happened on this page
https://fasterthanli.me/articles/request-coalescing-in-async-rust#a-bit-of-tracing
Here's what the issue is
I found a serious issue. You give this code:
I tried this code in my project, and it didn't work well. Only last message is saved to file. At first I thought that this is bug in
tracing
. So I wanted to report it totracing
. Then I discovered that.with_writer(File::create("/tmp/log.json").unwrap())
works well (i. e. saves all messages) and.with_writer(|| File::create("/tmp/log.json").unwrap())
doesn't (saves last message only).It seems if we pass closure to
with_writer
, then the closure is called every timetracing
wants to write something. So the file is truncated every time. So, please, write.with_writer(File::create("/tmp/log.json").unwrap())