Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

client: improve log formatting #7272

Merged
merged 1 commit into from
Oct 7, 2020
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
34 changes: 28 additions & 6 deletions client/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ use structopt::{
clap::{self, AppSettings},
StructOpt,
};
use tracing_subscriber::{filter::Directive, layer::SubscriberExt};
use tracing_subscriber::{
filter::Directive, fmt::time::ChronoLocal, layer::SubscriberExt, FmtSubscriber, Layer,
};

/// Substrate client CLI
///
Expand Down Expand Up @@ -255,8 +257,6 @@ pub fn init_logger(
// Set warn logging by default for some modules.
.add_directive("cranelift_wasm=warn".parse().expect("provided directive is valid"))
.add_directive("hyper=warn".parse().expect("provided directive is valid"))
// Always log the special target `sc_tracing`, overrides global level.
.add_directive("sc_tracing=trace".parse().expect("provided directive is valid"))
// Enable info for others.
.add_directive(tracing_subscriber::filter::LevelFilter::INFO.into());

Expand All @@ -278,15 +278,37 @@ pub fn init_logger(
}
}

// If we're only logging `INFO` entries then we'll use a simplified logging format.
let simple = match Layer::<FmtSubscriber>::max_level_hint(&env_filter) {
Some(level) if level <= tracing_subscriber::filter::LevelFilter::INFO => true,
_ => false,
};

// Always log the special target `sc_tracing`, overrides global level.
// NOTE: this must be done after we check the `max_level_hint` otherwise
// it is always raised to `TRACE`.
env_filter = env_filter.add_directive(
"sc_tracing=trace"
.parse()
.expect("provided directive is valid"),
);

let isatty = atty::is(atty::Stream::Stderr);
let enable_color = isatty;
let timer = ChronoLocal::with_format(if simple {
"%Y-%m-%d %H:%M:%S".to_string()
} else {
"%Y-%m-%d %H:%M:%S%.3f".to_string()
});

let subscriber = tracing_subscriber::FmtSubscriber::builder()
let subscriber = FmtSubscriber::builder()
.with_env_filter(env_filter)
.with_target(false)
.with_ansi(enable_color)
.with_target(!simple)
.with_level(!simple)
.with_thread_names(!simple)
.with_timer(timer)
.with_writer(std::io::stderr)
.compact()
.finish();

if let Some(tracing_targets) = tracing_targets {
Expand Down