Trait WithSubscriber

trait WithSubscriber: Sized

Extension trait allowing futures to be instrumented with a tracing Subscriber.

Provided Methods

fn with_subscriber<S>(self: Self, subscriber: S) -> WithDispatch<Self>
where
    S: Into<Dispatch>

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper.

The attached Subscriber will be set as the default when the returned Future is polled.

Examples

# use tracing::subscriber::NoSubscriber as MySubscriber;
# use tracing::subscriber::NoSubscriber as MyOtherSubscriber;
# async fn docs() {
use tracing::instrument::WithSubscriber;

// Set the default `Subscriber`
let _default = tracing::subscriber::set_default(MySubscriber::default());

tracing::info!("this event will be recorded by the default `Subscriber`");

// Create a different `Subscriber` and attach it to a future.
let other_subscriber = MyOtherSubscriber::default();
let future = async {
    tracing::info!("this event will be recorded by the other `Subscriber`");
    // ...
};

future
    // Attach the other `Subscriber` to the future before awaiting it
    .with_subscriber(other_subscriber)
    .await;

// Once the future has completed, we return to the default `Subscriber`.
tracing::info!("this event will be recorded by the default `Subscriber`");
# }
fn with_current_subscriber(self: Self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper.

The attached Subscriber will be set as the default when the returned Future is polled.

This can be used to propagate the current dispatcher context when spawning a new future that may run on a different thread.

Examples

# mod tokio {
#     pub(super) fn spawn(_: impl std::future::Future) {}
# }
# use tracing::subscriber::NoSubscriber as MySubscriber;
# async fn docs() {
use tracing::instrument::WithSubscriber;

// Using `set_default` (rather than `set_global_default`) sets the
// default `Subscriber` for *this* thread only.
let _default = tracing::subscriber::set_default(MySubscriber::default());

let future = async {
    // ...
};

// If a multi-threaded async runtime is in use, this spawned task may
// run on a different thread, in a different default `Subscriber`'s context.
tokio::spawn(future);

// However, calling `with_current_subscriber` on the future before
// spawning it, ensures that the current thread's default `Subscriber` is
// propagated to the spawned task, regardless of where it executes:
# let future = async { };
tokio::spawn(future.with_current_subscriber());
# }

Implementors