Skip to content

Commit

Permalink
Keep both stderr and file writers
Browse files Browse the repository at this point in the history
  • Loading branch information
akoshelev committed Dec 12, 2024
1 parent 6d54cac commit 07cac6d
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions ipa-core/src/cli/verbosity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,32 @@ impl Verbosity {
let filter_layer = self.log_filter();
info!("Logging setup at level {}", filter_layer);

let registry = tracing_subscriber::registry().with(filter_layer);
let stderr_writer = fmt::layer()
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
.with_ansi(std::io::stderr().is_terminal())
.with_writer(stderr);

let registry = tracing_subscriber::registry()
.with(filter_layer)
.with(stderr_writer);

if let Some(path) = &self.log_file {
let log_file = OpenOptions::new()
.append(true)
.create(true)
.open(path)
.unwrap_or_else(|e| panic!("failed to open log file {path:?}: {e}"));
let fmt_layer = fmt::layer()
let file_writer = fmt::layer()
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
.with_ansi(false)
.with_writer(log_file);

// that's the only stderr message that should appear to give a hint where
// the logs are written to
eprintln!("Logs will be written to {path:?}");
registry.with(fmt_layer).init();
tracing::info!("Logs will be written to {path:?}");
registry.with(file_writer).init();
} else {
let fmt_layer = fmt::layer()
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
.with_ansi(std::io::stderr().is_terminal())
.with_writer(stderr);
registry.with(fmt_layer).init();
registry.init();
}

let metrics_handle = install_collector().expect("Can install metrics");
Expand Down

0 comments on commit 07cac6d

Please sign in to comment.