39 lines
1.0 KiB
Rust
39 lines
1.0 KiB
Rust
use tokio::task::JoinHandle;
|
|
use tracing_subscriber::{
|
|
fmt::{MakeWriter, format::FmtSpan},
|
|
layer::SubscriberExt,
|
|
util::SubscriberInitExt,
|
|
};
|
|
|
|
pub fn init_subscriber<Sink>(sink: Sink)
|
|
where
|
|
Sink: for<'a> MakeWriter<'a> + Send + Sync + 'static,
|
|
{
|
|
tracing_subscriber::registry()
|
|
.with(
|
|
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
|
|
format!(
|
|
"{}=debug,tower_http=debug,axum::rejection=trace",
|
|
env!("CARGO_CRATE_NAME")
|
|
)
|
|
.into()
|
|
}),
|
|
)
|
|
.with(
|
|
tracing_subscriber::fmt::layer()
|
|
.pretty()
|
|
.with_writer(sink)
|
|
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE),
|
|
)
|
|
.init();
|
|
}
|
|
|
|
pub fn spawn_blocking_with_tracing<F, R>(f: F) -> JoinHandle<R>
|
|
where
|
|
F: FnOnce() -> R + Send + 'static,
|
|
R: Send + 'static,
|
|
{
|
|
let current_span = tracing::Span::current();
|
|
tokio::task::spawn_blocking(move || current_span.in_scope(f))
|
|
}
|