diff --git a/Cargo.lock b/Cargo.lock index e47a1fc098..5209394ebb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -850,6 +850,7 @@ dependencies = [ name = "millhone" version = "0.3.1" dependencies = [ + "atty", "base64 0.21.4", "clap 4.4.5", "derive_more", @@ -873,6 +874,7 @@ dependencies = [ "tikv-jemallocator", "traceconf", "tracing", + "tracing-subscriber", "typed-builder 0.16.2", "ureq", "url", diff --git a/extlib/millhone/Cargo.toml b/extlib/millhone/Cargo.toml index 44768f7edc..60822dd6d0 100644 --- a/extlib/millhone/Cargo.toml +++ b/extlib/millhone/Cargo.toml @@ -33,6 +33,8 @@ secrecy = "0.8.0" serde_json = "1.0.107" serde_yaml = "0.9.25" rayon = "1.8.0" +atty = "0.2.14" +tracing-subscriber = { version = "0.3.17", features = ["json"] } [dev-dependencies] maplit = "1.0.2" diff --git a/extlib/millhone/src/main.rs b/extlib/millhone/src/main.rs index 2cad626ee5..097816117f 100644 --- a/extlib/millhone/src/main.rs +++ b/extlib/millhone/src/main.rs @@ -7,7 +7,9 @@ use clap::{Parser, Subcommand}; use millhone::url::BaseUrl; use stable_eyre::eyre::Context; -use traceconf::TracingConfig; +use tap::Pipe; +use traceconf::{Colors, Format, Level}; +use tracing_subscriber::prelude::*; mod cmd; @@ -23,9 +25,17 @@ static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; #[derive(Debug, Parser)] #[clap(version)] struct Application { - /// Tracing configuration. - #[clap(flatten)] - tracing: TracingConfig, + /// Set the minimum level for logs. Logs below this level are dropped. + #[clap(long, default_value_t = Level::default())] + log_level: Level, + + /// The formatter to use for logs. + #[clap(long, default_value_t = Format::default())] + log_format: Format, + + /// The coloring mode to use for log and span traces. + #[clap(long, default_value_t = Colors::default())] + log_colors: Colors, /// The URL for the Millhone service. /// @@ -49,6 +59,22 @@ struct Application { commands: Commands, } +impl Application { + /// Determine whether colors should be enabled for tracing output. + pub fn colors_enabled(&self) -> bool { + match self.log_colors { + Colors::Auto => atty::is(atty::Stream::Stderr), + Colors::Enable => true, + _ => false, + } + } + + /// Translate the level selected by the user into the format used by [`tracing`]. + pub fn level_filter(&self) -> tracing_subscriber::filter::LevelFilter { + self.log_level.into() + } +} + #[derive(Debug, Subcommand)] enum Commands { /// Ping the Millhone backend. @@ -69,10 +95,31 @@ fn main() -> stable_eyre::Result<()> { stable_eyre::install()?; let app = Application::parse(); - // Set up tracing according to the defaults. - // We can customize this later if desired. - let subscriber = app.tracing.subscriber(); - tracing::subscriber::set_global_default(subscriber).context("install tracing")?; + match app.log_format { + Format::Json => tracing_subscriber::Registry::default() + .with( + tracing_subscriber::fmt::layer() + .json() + .with_ansi(app.colors_enabled()) + .with_writer(std::io::stdout) + .with_span_events(tracing_subscriber::fmt::format::FmtSpan::ACTIVE) + .with_filter(app.level_filter()), + ) + .pipe(tracing::subscriber::set_global_default) + .context("install tracing")?, + _ => tracing_subscriber::Registry::default() + .with( + tracing_subscriber::fmt::layer() + .with_ansi(app.colors_enabled()) + .with_writer(std::io::stdout) + .with_file(false) + .with_line_number(false) + .with_span_events(tracing_subscriber::fmt::format::FmtSpan::NONE) + .with_filter(app.level_filter()), + ) + .pipe(tracing::subscriber::set_global_default) + .context("install tracing")?, + } // And then dispatch to the subcommand. match app.commands {