schema_registry_cli/
init_tracing.rs

1use tracing::metadata::LevelFilter;
2use tracing_subscriber::fmt::format::FmtSpan;
3
4use crate::{CliError, Result, Verbosity};
5
6/// Initialise tracing
7///
8/// # Errors
9///
10/// Fail if the tracing have already been globally configured
11pub fn init_tracing(verbosity: Verbosity) -> Result<()> {
12    let filter = LevelFilter::from(verbosity);
13    tracing_subscriber::fmt()
14        .pretty()
15        .with_line_number(true)
16        .with_span_events(FmtSpan::ACTIVE)
17        .with_max_level(filter)
18        .try_init()
19        .map_err(|err| CliError::InitTracingError(err.to_string()))
20}
21
22impl From<Verbosity> for LevelFilter {
23    fn from(value: Verbosity) -> Self {
24        match i16::from(value) {
25            i16::MIN..=-2 => LevelFilter::OFF,
26            -1 => LevelFilter::ERROR,
27            0 => LevelFilter::WARN,
28            1 => LevelFilter::INFO,
29            2 => LevelFilter::DEBUG,
30            3.. => LevelFilter::TRACE,
31        }
32    }
33}