Skip to content

Commit

Permalink
Introduce NL_LOG to control logging format (#1154)
Browse files Browse the repository at this point in the history
`NL_LOG` envionment variable is used to control
the logging format that `tracing_subscriber`
emits out. We currently want to allow for better
ingestion of formatted logs in json.

`NL_LOG` can be configured with `pretty`, `json`,
or `compact`.
  • Loading branch information
adam-singer authored Jul 13, 2024
1 parent 53215a9 commit d9922b3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion nativelink-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
52 changes: 37 additions & 15 deletions nativelink-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit d9922b3

Please sign in to comment.