mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 15:47:58 +00:00
Use tracing-based subscriber logging (#6825)
* init_logger: switch from log-based to tracing-based and add compatibility layer * Move tracing profiling subscriber related config realization * sp-tracing: change profiling to be a layer instead of a subscriber * Enable profiling layer in cli * Change all test env_logger init to sp_tracing::try_init_simple * Remove all local env_logger dependency * Add missing tracing-subscriber dependency * frame-sudo: fix tests * frame-support: fix tests * Fix frame/pallet and executor tests * Fix the remaining tests * Use subscriber's try_init as recommended by @davidbarsky * Be explict that the tracing-log feature is needed * Set subscriber writer to stderr * Shorter line width * Update cargo lock tracing version * Fix sc_tracing crate compile * Fix sc_authority_discovery crate test * unremove default-features * Leave enabled to default true * Warn if global default cannot be set * Fix unused import * Remove unused PROXY_TARGET * Change all reference from rc5 to rc6 * Change all reference of rc2 to rc6 * Fix styling * Fix typo * make logger init error'ing * re-fixing the test issue Co-authored-by: Benjamin Kampmann <ben@parity.io>
This commit is contained in:
@@ -32,10 +32,7 @@ pub use arg_enums::*;
|
||||
pub use commands::*;
|
||||
pub use config::*;
|
||||
pub use error::*;
|
||||
use lazy_static::lazy_static;
|
||||
use log::info;
|
||||
pub use params::*;
|
||||
use regex::Regex;
|
||||
pub use runner::*;
|
||||
use sc_service::{Configuration, TaskExecutor};
|
||||
pub use sc_service::{ChainSpec, Role};
|
||||
@@ -46,6 +43,7 @@ use structopt::{
|
||||
clap::{self, AppSettings},
|
||||
StructOpt,
|
||||
};
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
|
||||
/// Substrate client CLI
|
||||
///
|
||||
@@ -228,79 +226,76 @@ pub trait SubstrateCli: Sized {
|
||||
fn native_runtime_version(chain_spec: &Box<dyn ChainSpec>) -> &'static RuntimeVersion;
|
||||
}
|
||||
|
||||
/// Initialize the logger
|
||||
pub fn init_logger(pattern: &str) {
|
||||
use ansi_term::Colour;
|
||||
|
||||
let mut builder = env_logger::Builder::new();
|
||||
// Disable info logging by default for some modules:
|
||||
builder.filter(Some("ws"), log::LevelFilter::Off);
|
||||
builder.filter(Some("yamux"), log::LevelFilter::Off);
|
||||
builder.filter(Some("cranelift_codegen"), log::LevelFilter::Off);
|
||||
builder.filter(Some("hyper"), log::LevelFilter::Warn);
|
||||
builder.filter(Some("cranelift_wasm"), log::LevelFilter::Warn);
|
||||
// Always log the special target `sc_tracing`, overrides global level
|
||||
builder.filter(Some("sc_tracing"), log::LevelFilter::Trace);
|
||||
// Enable info for others.
|
||||
builder.filter(None, log::LevelFilter::Info);
|
||||
|
||||
if let Ok(lvl) = std::env::var("RUST_LOG") {
|
||||
builder.parse_filters(&lvl);
|
||||
/// Initialize the global logger
|
||||
///
|
||||
/// This sets various global logging and tracing instances and thus may only be called once.
|
||||
pub fn init_logger(
|
||||
pattern: &str,
|
||||
tracing_receiver: sc_tracing::TracingReceiver,
|
||||
tracing_targets: Option<String>,
|
||||
) -> std::result::Result<(), String> {
|
||||
if let Err(e) = tracing_log::LogTracer::init() {
|
||||
return Err(format!(
|
||||
"Registering Substrate logger failed: {:}!", e
|
||||
))
|
||||
}
|
||||
|
||||
let mut env_filter = tracing_subscriber::EnvFilter::default()
|
||||
// Disable info logging by default for some modules.
|
||||
.add_directive("ws=off".parse().expect("provided directive is valid"))
|
||||
.add_directive("yamux=off".parse().expect("provided directive is valid"))
|
||||
.add_directive("cranelift_codegen=off".parse().expect("provided directive is valid"))
|
||||
// Set warn logging by default for some modules.
|
||||
.add_directive("cranelife_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());
|
||||
|
||||
if let Ok(lvl) = std::env::var("RUST_LOG") {
|
||||
if lvl != "" {
|
||||
// We're not sure if log or tracing is available at this moment, so silently ignore the
|
||||
// parse error.
|
||||
if let Ok(directive) = lvl.parse() {
|
||||
env_filter = env_filter.add_directive(directive);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if pattern != "" {
|
||||
// We're not sure if log or tracing is available at this moment, so silently ignore the
|
||||
// parse error.
|
||||
if let Ok(directive) = pattern.parse() {
|
||||
env_filter = env_filter.add_directive(directive);
|
||||
}
|
||||
}
|
||||
|
||||
builder.parse_filters(pattern);
|
||||
let isatty = atty::is(atty::Stream::Stderr);
|
||||
let enable_color = isatty;
|
||||
|
||||
builder.format(move |buf, record| {
|
||||
let now = time::now();
|
||||
let timestamp =
|
||||
time::strftime("%Y-%m-%d %H:%M:%S", &now).expect("Error formatting log timestamp");
|
||||
let subscriber = tracing_subscriber::FmtSubscriber::builder()
|
||||
.with_env_filter(env_filter)
|
||||
.with_target(false)
|
||||
.with_ansi(enable_color)
|
||||
.with_writer(std::io::stderr)
|
||||
.compact()
|
||||
.finish();
|
||||
|
||||
let mut output = if log::max_level() <= log::LevelFilter::Info {
|
||||
format!(
|
||||
"{} {}",
|
||||
Colour::Black.bold().paint(timestamp),
|
||||
record.args(),
|
||||
)
|
||||
} else {
|
||||
let name = ::std::thread::current()
|
||||
.name()
|
||||
.map_or_else(Default::default, |x| {
|
||||
format!("{}", Colour::Blue.bold().paint(x))
|
||||
});
|
||||
let millis = (now.tm_nsec as f32 / 1000000.0).floor() as usize;
|
||||
let timestamp = format!("{}.{:03}", timestamp, millis);
|
||||
format!(
|
||||
"{} {} {} {} {}",
|
||||
Colour::Black.bold().paint(timestamp),
|
||||
name,
|
||||
record.level(),
|
||||
record.target(),
|
||||
record.args()
|
||||
)
|
||||
};
|
||||
if let Some(tracing_targets) = tracing_targets {
|
||||
let profiling = sc_tracing::ProfilingLayer::new(tracing_receiver, &tracing_targets);
|
||||
|
||||
if !isatty && record.level() <= log::Level::Info && atty::is(atty::Stream::Stdout) {
|
||||
// duplicate INFO/WARN output to console
|
||||
println!("{}", output);
|
||||
if let Err(e) = tracing::subscriber::set_global_default(subscriber.with(profiling)) {
|
||||
return Err(format!(
|
||||
"Registering Substrate tracing subscriber failed: {:}!", e
|
||||
))
|
||||
}
|
||||
|
||||
if !enable_color {
|
||||
output = kill_color(output.as_ref());
|
||||
} else {
|
||||
if let Err(e) = tracing::subscriber::set_global_default(subscriber) {
|
||||
return Err(format!(
|
||||
"Registering Substrate tracing subscriber failed: {:}!", e
|
||||
))
|
||||
}
|
||||
|
||||
writeln!(buf, "{}", output)
|
||||
});
|
||||
|
||||
if builder.try_init().is_err() {
|
||||
info!("💬 Not registering Substrate logger, as there is already a global logger registered!");
|
||||
}
|
||||
}
|
||||
|
||||
fn kill_color(s: &str) -> String {
|
||||
lazy_static! {
|
||||
static ref RE: Regex = Regex::new("\x1b\\[[^m]+m").expect("Error initializing color regex");
|
||||
}
|
||||
RE.replace_all(s, "").to_string()
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user