Trait tracing::instrument::WithSubscriber
source · [−]pub trait WithSubscriber: Sized {
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T: Future> Future for WithDispatch<T> type Output = T::Output;
where
S: Into<Dispatch>,
{ ... }
fn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T: Future> Future for WithDispatch<T> type Output = T::Output;
{ ... }
}
Expand description
Extension trait allowing futures to be instrumented with
a tracing
[Subscriber
].
Provided methods
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T: Future> Future for WithDispatch<T> type Output = T::Output;
where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T: Future> Future for WithDispatch<T> type Output = T::Output;
where
S: Into<Dispatch>,
Notable traits for WithDispatch<T>
impl<T: Future> Future for WithDispatch<T> type Output = T::Output;
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::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) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T: Future> Future for WithDispatch<T> type Output = T::Output;
fn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T: Future> Future for WithDispatch<T> type Output = T::Output;
Notable traits for WithDispatch<T>
impl<T: Future> Future for WithDispatch<T> type Output = T::Output;
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
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:
tokio::spawn(future.with_current_subscriber());