From ae6faf59e2a17c0abfd9e905dd68095b11568eab Mon Sep 17 00:00:00 2001 From: Andy Lok Date: Fri, 2 Aug 2024 04:40:12 +0800 Subject: [PATCH] chore: reduce duplicated functions (#20) --- src/append/mod.rs | 7 ------- src/logger.rs | 32 +++----------------------------- 2 files changed, 3 insertions(+), 36 deletions(-) diff --git a/src/append/mod.rs b/src/append/mod.rs index 97c3818..8fbf872 100644 --- a/src/append/mod.rs +++ b/src/append/mod.rs @@ -20,7 +20,6 @@ pub use fastrace::*; pub use opentelemetry::*; pub use stdio::*; -use crate::filter::Filter; use crate::layout::IdenticalLayout; use crate::layout::Layout; @@ -44,10 +43,4 @@ pub trait Append: fmt::Debug + Send + Sync + 'static { fn default_layout(&self) -> Layout { Layout::Identical(IdenticalLayout) } - - /// Default filters associated to this append. [log::Log] is mixed with - /// [Filter] and [Append]. - fn default_filters(&self) -> Vec { - vec![] - } } diff --git a/src/logger.rs b/src/logger.rs index 4a78176..6f6310a 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -56,27 +56,9 @@ impl Dispatch { layout: Some(layout.into()), } } - - pub fn append(self, append: impl Append) -> Dispatch { - Dispatch { - filters: self.filters, - appends: vec![Box::new(append)], - layout: self.layout, - } - } -} - -impl Dispatch { - pub fn append(self, append: impl Append) -> Dispatch { - Dispatch { - filters: self.filters, - appends: vec![Box::new(append)], - layout: self.layout, - } - } } -impl Dispatch { +impl Dispatch { pub fn append(self, append: impl Append) -> Dispatch { Dispatch { filters: self.filters, @@ -99,18 +81,10 @@ impl Dispatch { true } - fn do_append(&self, record: &Record) -> anyhow::Result<()> { + fn log(&self, record: &Record) -> anyhow::Result<()> { let layout = self.layout.as_ref(); for append in &self.appends { - for filter in append.default_filters() { - match filter.filter(record.metadata()) { - FilterResult::Reject => return Ok(()), - FilterResult::Accept => break, - FilterResult::Neutral => {} - } - } - match layout { Some(layout) => layout.format(record, &|record| append.append(record))?, None => append @@ -165,7 +139,7 @@ impl log::Log for Logger { fn log(&self, record: &Record) { for dispatch in &self.dispatches { if dispatch.enabled(record.metadata()) { - if let Err(err) = dispatch.do_append(record) { + if let Err(err) = dispatch.log(record) { handle_error(record, err); } }