pub trait FutureExt: Sized + Sealed {
// Required method
fn in_log_context(self, context: LogContext) -> LogContextFuture<Self> ⓘ;
}
Expand description
Extension trait for futures to propagate contextual logging information.
This trait adds ability to attach a LogContext
for any Future
,
ensuring that logs emitted during the future’s execution will include
the contextual properties even if the future is polled across different threads.
Required Methods§
Sourcefn in_log_context(self, context: LogContext) -> LogContextFuture<Self> ⓘ
fn in_log_context(self, context: LogContext) -> LogContextFuture<Self> ⓘ
Attaches a log context to this future.
The attached context will be activated every time the instrumented future is polled.
§Examples
use context_logger::{LogContext, FutureExt};
use log::info;
async fn process_user_data(user_id: u64) {
// Create a context with user information
let context = LogContext::new()
.record("user_id", user_id)
.record("operation", "process_data");
async {
info!("Starting user data processing"); // Will include context
// Do some async work...
info!("User data processing complete"); // Still includes context
}
.in_log_context(context)
.await;
}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.