From 5fdbcbf61da27ec3e600678121d8c00d2b9b5cb1 Mon Sep 17 00:00:00 2001 From: LIU JIE <38575932+Millione@users.noreply.github.com> Date: Sat, 11 Sep 2021 08:56:03 +0800 Subject: [PATCH] log: fix `LogTracer` not honoring max level filters (#1543) Co-authored-by: Eliza Weisman --- tracing-log/src/log_tracer.rs | 4 +++- tracing-log/tests/log_tracer.rs | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tracing-log/src/log_tracer.rs b/tracing-log/src/log_tracer.rs index b354a0103e..4d868d1664 100644 --- a/tracing-log/src/log_tracer.rs +++ b/tracing-log/src/log_tracer.rs @@ -183,7 +183,9 @@ impl log::Log for LogTracer { } fn log(&self, record: &log::Record<'_>) { - crate::dispatch_record(record); + if self.enabled(record.metadata()) { + crate::dispatch_record(record); + } } fn flush(&self) {} diff --git a/tracing-log/tests/log_tracer.rs b/tracing-log/tests/log_tracer.rs index ee367e07a6..79d3f45cac 100644 --- a/tracing-log/tests/log_tracer.rs +++ b/tracing-log/tests/log_tracer.rs @@ -1,7 +1,7 @@ use std::sync::{Arc, Mutex}; use tracing::collect::with_default; use tracing_core::span::{Attributes, Record}; -use tracing_core::{span, Collect, Event, Level, Metadata}; +use tracing_core::{span, Collect, Event, Level, LevelFilter, Metadata}; use tracing_log::{LogTracer, NormalizeEvent}; struct State { @@ -26,6 +26,10 @@ impl Collect for TestSubscriber { true } + fn max_level_hint(&self) -> Option { + Some(LevelFilter::from_level(Level::INFO)) + } + fn new_span(&self, _span: &Attributes<'_>) -> span::Id { span::Id::from_u64(42) } @@ -67,6 +71,8 @@ fn normalized_metadata() { let state = me.clone(); with_default(TestSubscriber(me), || { + log::info!("expected info log"); + log::debug!("unexpected debug log"); let log = log::Record::builder() .args(format_args!("Error!")) .level(log::Level::Info)