docspec_http/tracing_init.rs
1//! Tracing subscriber initialization.
2
3use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _, Layer as _};
4
5/// Installs a hardcoded tracing subscriber writing to stderr at INFO level with pretty format.
6///
7/// # Panics
8///
9/// Panics if a global subscriber has already been installed. In tests, use [`try_init`] instead.
10#[inline]
11pub fn init() {
12 tracing_subscriber::registry()
13 .with(
14 tracing_subscriber::fmt::layer()
15 .with_writer(std::io::stderr)
16 .pretty()
17 .with_filter(tracing::level_filters::LevelFilter::INFO),
18 )
19 .with(crate::telemetry::tracing_layer())
20 .init();
21}
22
23/// Installs the tracing subscriber, returning an error if one is already set.
24///
25/// Use this in tests to avoid panics from double-initialization.
26///
27/// # Errors
28///
29/// Returns [`Err`] if a global subscriber has already been installed.
30#[inline]
31#[allow(clippy::std_instead_of_core)]
32// Reason: `std::error::Error` is not available in `core`; the `tracing_subscriber`
33// crate's `try_init` error type requires `std::error::Error + Send + Sync`.
34pub fn try_init() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
35 tracing_subscriber::registry()
36 .with(
37 tracing_subscriber::fmt::layer()
38 .with_writer(std::io::stderr)
39 .pretty()
40 .with_filter(tracing::level_filters::LevelFilter::INFO),
41 )
42 .with(crate::telemetry::tracing_layer())
43 .try_init()
44 .map_err(Into::into)
45}