Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce NL_LOG to control logging format #1154

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nativelink-util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
52 changes: 37 additions & 15 deletions nativelink-util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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(())
}