1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use anyhow::Result;
use tracing::{
    level_filters::LevelFilter,
    subscriber::{self, Subscriber},
};
use tracing_subscriber::EnvFilter;

pub fn setup_logging() -> Result<()> {
    subscriber::set_global_default(subscriber())?;

    Ok(())
}

#[cfg(not(feature = "tracing-forest"))]
pub fn subscriber() -> impl Subscriber + core::fmt::Debug {
    use tracing_subscriber::fmt::format::FmtSpan;

    tracing_subscriber::fmt()
        .pretty()
        .compact()
        .with_level(true)
        .with_file(true)
        .with_line_number(true)
        .with_target(true)
        .with_env_filter(
            EnvFilter::builder()
                .with_default_directive(LevelFilter::INFO.into())
                .from_env_lossy(),
        )
        .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
        .finish()
}

#[cfg(feature = "tracing-forest")]
pub fn subscriber() -> impl Subscriber + core::fmt::Debug {
    pub use tracing_forest::ForestLayer;
    pub use tracing_subscriber::{layer::SubscriberExt, Registry};

    Registry::default().with(ForestLayer::default()).with(
        EnvFilter::builder()
            .with_default_directive(LevelFilter::INFO.into())
            .from_env_lossy(),
    )
}