subspace_logging/
lib.rs

1use tracing::level_filters::LevelFilter;
2use tracing_subscriber::layer::SubscriberExt;
3use tracing_subscriber::util::SubscriberInitExt;
4use tracing_subscriber::{fmt, EnvFilter, Layer};
5
6pub fn init_logger() {
7    // TODO: Workaround for https://github.com/tokio-rs/tracing/issues/2214, also on
8    //  Windows terminal doesn't support the same colors as bash does
9    let enable_color = if cfg!(windows) {
10        false
11    } else {
12        supports_color::on(supports_color::Stream::Stderr).is_some()
13    };
14
15    let res = tracing_subscriber::registry()
16        .with(
17            fmt::layer().with_ansi(enable_color).with_filter(
18                EnvFilter::builder()
19                    .with_default_directive(LevelFilter::INFO.into())
20                    .from_env_lossy(),
21            ),
22        )
23        .try_init();
24
25    if let Err(e) = res {
26        // In production, this might be a bug in the logging setup.
27        // In some tests, it is expected.
28        eprintln!(
29            "Failed to initialize logger: {}. \
30            This is expected when running nexttest test functions under `cargo test`.",
31            e
32        );
33    }
34}