Small fix for log line prefix (#7373)

This commit is contained in:
Cecile Tonglet
2020-10-22 12:02:16 +02:00
committed by GitHub
parent 0fcf9d6c60
commit d847c9b019
2 changed files with 13 additions and 74 deletions
+10 -2
View File
@@ -423,6 +423,11 @@ mod tests {
#[test]
fn prefix_in_log_lines() {
let re = regex::Regex::new(&format!(
r"^\d{{4}}-\d{{2}}-\d{{2}} \d{{2}}:\d{{2}}:\d{{2}} \[{}\] {}$",
EXPECTED_NODE_NAME,
EXPECTED_LOG_MESSAGE,
)).unwrap();
let executable = env::current_exe().unwrap();
let output = Command::new(executable)
.env("ENABLE_LOGGING", "1")
@@ -431,7 +436,10 @@ mod tests {
.unwrap();
let output = String::from_utf8(output.stderr).unwrap();
assert!(output.contains(&format!(" [{}] ", EXPECTED_NODE_NAME)));
assert!(
re.is_match(output.trim()),
format!("Expected:\n{}\nGot:\n{}", re, output),
);
}
/// This is no actual test, it will be used by the `prefix_in_log_lines` test.
@@ -448,6 +456,6 @@ mod tests {
#[crate::prefix_logs_with(EXPECTED_NODE_NAME)]
fn prefix_in_log_lines_process() {
log::info!("Hello World!");
log::info!("{}", EXPECTED_LOG_MESSAGE);
}
}
+3 -72
View File
@@ -16,12 +16,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use ansi_term::{Colour, Style};
use std::{fmt::{self, Write as _}, iter};
use tracing::{
span::{self, Attributes},
Event, Id, Level, Subscriber,
};
use ansi_term::Colour;
use std::fmt;
use tracing::{span::Attributes, Event, Id, Level, Subscriber};
use tracing_log::NormalizeEvent;
use tracing_subscriber::{
fmt::{
@@ -93,8 +90,6 @@ where
}
}
let fmt_ctx = { FmtCtx::new(&ctx, event.parent(), self.ansi) };
write!(writer, "{}", fmt_ctx)?;
if self.display_target {
write!(writer, "{}:", meta.target())?;
}
@@ -247,70 +242,6 @@ impl<'a> fmt::Display for FmtThreadName<'a> {
}
}
struct FmtCtx<'a, S, N> {
ctx: &'a FmtContext<'a, S, N>,
span: Option<&'a span::Id>,
ansi: bool,
}
impl<'a, S, N: 'a> FmtCtx<'a, S, N>
where
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
N: for<'writer> FormatFields<'writer> + 'static,
{
pub(crate) fn new(
ctx: &'a FmtContext<'_, S, N>,
span: Option<&'a span::Id>,
ansi: bool,
) -> Self {
Self { ctx, ansi, span }
}
fn bold(&self) -> Style {
if self.ansi {
return Style::new().bold();
}
Style::new()
}
}
// NOTE: the following code took inspiration from tracing-subscriber
//
// https://github.com/tokio-rs/tracing/blob/2f59b32/tracing-subscriber/src/fmt/format/mod.rs#L711
impl<'a, S, N: 'a> fmt::Display for FmtCtx<'a, S, N>
where
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
N: for<'writer> FormatFields<'writer> + 'static,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let bold = self.bold();
let mut seen = false;
let span = self
.span
.and_then(|id| self.ctx.span(&id))
.or_else(|| self.ctx.lookup_current());
let scope = span
.into_iter()
.flat_map(|span| span.from_root().chain(iter::once(span)));
for name in scope
.map(|span| span.metadata().name())
.filter(|&x| x != "substrate-node")
{
seen = true;
write!(f, "{}:", bold.paint(name))?;
}
if seen {
f.write_char(' ')?;
}
Ok(())
}
}
// NOTE: the following code has been duplicated from tracing-subscriber
//
// https://github.com/tokio-rs/tracing/blob/2f59b32/tracing-subscriber/src/fmt/time/mod.rs#L252