fasterthanlime / feedback

An issue tracker for articles/series/videos at https://fasterthanli.me/
13 stars 0 forks source link

"Request coalescing in async Rust": .with_writer(File::create...) should be #276

Closed safinaskar closed 6 months ago

safinaskar commented 11 months ago

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:

            tracing_subscriber::fmt::layer()
                .json()
                .with_writer(|| File::create("/tmp/log.json").unwrap()),

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())

fasterthanlime commented 6 months ago

Nice catch! I updated the article following your suggestions.