Skip to content

Commit

Permalink
Set default log directives only when RUST_LOG is not set
Browse files Browse the repository at this point in the history
Summary:
Normally, [log directives are set using the RUST_LOG environment variable](https://docs.rs/env_logger/latest/env_logger/#enabling-logging). We have some default log directives we want to set in supertd, and we currently set them unconditionally, which is surprising to someone expecting directives they assign to the RUST_LOG envvar to work! Instead, only set the default log directives when RUST_LOG is not set.

This diff also moves the place where we set the default log directives into init_tracing, so that we have uniform logging behavior across td binaries.

Reviewed By: Acesine

Differential Revision: D53673935

fbshipit-source-id: 5e5564a8c9c42488312f6220302c0310b631a560
  • Loading branch information
rjbailey authored and facebook-github-bot committed Feb 13, 2024
1 parent 9334c4b commit 4d5dfbf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion btd/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ use td_util::cli::parse_args;
use td_util::tracing::init_tracing;

pub fn main() -> anyhow::Result<()> {
init_tracing(vec![]);
init_tracing();
btd::main(parse_args()?)
}
11 changes: 1 addition & 10 deletions supertd/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,7 @@ pub fn main(fb: FacebookInit) -> anyhow::Result<()> {
// Avoid a warning in open source code
let _unused_in_oss = fb;

// Enable info log by default, debug log for target determinator packages
init_tracing(vec![
"info".parse()?,
"btd=debug".parse()?,
"citadel=debug".parse()?,
"ranker=debug".parse()?,
"scheduler=debug".parse()?,
"targets=debug".parse()?,
"verse=debug".parse()?,
]);
init_tracing();

let mut command = Args::command();
if std::env::var_os("SUPERTD_IGNORE_EXTRA_ARGUMENTS") == Some("1".into()) {
Expand Down
2 changes: 1 addition & 1 deletion targets/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ use td_util::cli::parse_args;
use td_util::tracing::init_tracing;

pub fn main() -> anyhow::Result<()> {
init_tracing(vec![]);
init_tracing();
targets::main(parse_args()?)
}
23 changes: 17 additions & 6 deletions td_util/src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,30 @@ use std::io::stderr;
use std::io::stdout;
use std::io::IsTerminal;

use tracing::metadata::LevelFilter;
use tracing_subscriber::filter::Directive;
use tracing_subscriber::filter::EnvFilter;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::Layer;

/// Set up tracing so it prints to stderr, and can be used for output.
/// Most things should use `info` and `debug` level for showing messages.
pub fn init_tracing(directives: Vec<Directive>) {
let mut env_filter = EnvFilter::from_default_env().add_directive(LevelFilter::DEBUG.into());
for directive in directives {
env_filter = env_filter.add_directive(directive);
pub fn init_tracing() {
let mut env_filter = EnvFilter::from_default_env();
if std::env::var_os("RUST_LOG").is_none() {
// Enable info log by default, debug log for target determinator packages
let directives = vec![
"info",
"btd=debug",
"citadel=debug",
"ranker=debug",
"scheduler=debug",
"targets=debug",
"verse=debug",
];
for directive in directives {
env_filter =
env_filter.add_directive(directive.parse().expect("bad hardcoded log directive"));
}
}

let layer = tracing_subscriber::fmt::layer()
Expand Down

0 comments on commit 4d5dfbf

Please sign in to comment.