diff --git a/nativelink-util/Cargo.toml b/nativelink-util/Cargo.toml index 679bc51fc..c4737cc4a 100644 --- a/nativelink-util/Cargo.toml +++ b/nativelink-util/Cargo.toml @@ -30,7 +30,7 @@ tokio-stream = { version = "0.1.15", features = ["sync"] } tokio-util = { version = "0.7.11" } tonic = { version = "0.11.0", features = ["tls"] } tracing = "0.1.40" -tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } +tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] } uuid = { version = "1.8.0", features = ["v4", "serde"] } [dev-dependencies] diff --git a/nativelink-util/src/lib.rs b/nativelink-util/src/lib.rs index 717985274..2811a4d68 100644 --- a/nativelink-util/src/lib.rs +++ b/nativelink-util/src/lib.rs @@ -53,22 +53,44 @@ pub fn init_tracing() -> Result<(), nativelink_error::Error> { .with_default_directive(tracing::metadata::LevelFilter::WARN.into()) .from_env_lossy(); + // Setup tracing logger for multiple format types, compact, json, and pretty as a single layer. + // Configuration for log format comes from environment variable NL_LOG_FMT due to subscribers + // being configured before config parsing. + let nl_log_fmt = std::env::var("NL_LOG").unwrap_or_else(|_| "pretty".to_string()); + // Layers vector is used for due to how tracing_subscriber::fmt::layer builds type signature + // not being able to unify a single trait type before being boxed. For example see + // https://docs.rs/tracing-subscriber/0.3.18/tracing_subscriber/layer/index.html + let mut layers = Vec::new(); + match nl_log_fmt.as_str() { + "compact" => layers.push( + tracing_subscriber::fmt::layer() + .compact() + .with_timer(tracing_subscriber::fmt::time::time()) + .with_filter(env_filter) + .boxed(), + ), + "json" => layers.push( + tracing_subscriber::fmt::layer() + .json() + .with_timer(tracing_subscriber::fmt::time::time()) + .with_filter(env_filter) + .boxed(), + ), + _ => layers.push( + tracing_subscriber::fmt::layer() + .pretty() + .with_timer(tracing_subscriber::fmt::time::time()) + .with_filter(env_filter) + .boxed(), + ), + }; + + // Add a console subscriber if the feature is enabled, see tokio-console for a client console. + // https://crates.io/crates/tokio-console if cfg!(feature = "enable_tokio_console") { - tracing_subscriber::registry() - .with(console_subscriber::spawn()) - .with( - tracing_subscriber::fmt::layer() - .pretty() - .with_timer(tracing_subscriber::fmt::time::time()) - .with_filter(env_filter), - ) - .init(); - } else { - tracing_subscriber::fmt() - .pretty() - .with_timer(tracing_subscriber::fmt::time::time()) - .with_env_filter(env_filter) - .init(); + layers.push(console_subscriber::spawn().boxed()); } + + tracing_subscriber::registry().with(layers).init(); Ok(()) }