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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::args::{Colors, TaploArgs};
use std::io;
use taplo_common::environment::Environment;
use tracing_subscriber::{
    fmt::format::FmtSpan, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt,
    EnvFilter,
};

pub fn setup_stderr_logging(e: impl Environment, taplo: &TaploArgs) {
    let span_events = if taplo.log_spans {
        FmtSpan::NEW | FmtSpan::CLOSE
    } else {
        FmtSpan::NONE
    };

    let registry = tracing_subscriber::registry();

    let env_filter = match e.env_var("RUST_LOG") {
        Some(log) => EnvFilter::new(log),
        None => EnvFilter::default().add_directive(tracing::Level::INFO.into()),
    };

    if taplo.verbose {
        registry
            .with(env_filter)
            .with(
                tracing_subscriber::fmt::layer()
                    .with_ansi(match taplo.colors {
                        Colors::Auto => e.atty_stderr(),
                        Colors::Always => true,
                        Colors::Never => false,
                    })
                    .with_span_events(span_events)
                    .event_format(tracing_subscriber::fmt::format().pretty().with_ansi(
                        match taplo.colors {
                            Colors::Auto => e.atty_stderr(),
                            Colors::Always => true,
                            Colors::Never => false,
                        },
                    ))
                    .with_writer(io::stderr),
            )
            .init();
    } else {
        registry
            .with(env_filter)
            .with(
                tracing_subscriber::fmt::layer()
                    .with_ansi(match taplo.colors {
                        Colors::Auto => e.atty_stderr(),
                        Colors::Always => true,
                        Colors::Never => false,
                    })
                    .event_format(
                        tracing_subscriber::fmt::format()
                            .compact()
                            .with_source_location(false)
                            .with_target(false)
                            .without_time()
                            .with_ansi(match taplo.colors {
                                Colors::Auto => e.atty_stderr(),
                                Colors::Always => true,
                                Colors::Never => false,
                            }),
                    )
                    .without_time()
                    .with_file(false)
                    .with_line_number(false)
                    .with_span_events(span_events)
                    .with_writer(io::stderr),
            )
            .init();
    }
}