Using tracing's `.with_writer` with tokio

Will writing to a std::fs::File via .with_writer inside a tokio context block the executor, or is it on another thread?

Where are you getting the with_writer() method from? I can't find any such method/extension trait in tokio or std.

From tracing_subscriber. Sorry, should've said it in the body of the post.

Looking at this function, I think it writes to a temporary thread-local buffer before asking the underlying destination for a std::io::Write object it can synchronously write to.

Digging through the source more, I can find a direct line of execution between tracing::event! and tracing_subscriber::fmt::Layer::on_event. During that, there does not appear to be a point where it send the writing to be done on another thread. So it seems no, it isn't async-friendly.

Call path
  1. tracing::event!
  2. tracing::Event::child_of
  3. tracing::dispatch::get_default
  4. tracing::Dispatch::event
  5. <tracing_subcriber::Subscriber as tracing::Subscriber>::event
  6. <tracing_subscriber::fmt::Layer as tracing_subscriber::layer::Layer>::on_event
1 Like

(Hi, I help maintain tracing).

By default, tracing_subscriber::fmt::{Layer, Subscriber} will not write anything on different threads. If you need to write to a file on a different thread, we recommend using tracing-appender crates, which spins up a dedicated thread for writing to a file.

1 Like

Oh, this is exactly what I was hoping for! Thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.