From eebc2fe046ae61b739713366148edbb82c254909 Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 2 Aug 2024 08:05:27 +0800 Subject: [PATCH] chore: tidy some interfaces and naming (#22) Signed-off-by: tison --- Cargo.toml | 2 +- src/filter/min_level.rs | 25 +++++++++++++++++++------ src/filter/mod.rs | 6 +++--- src/logger.rs | 28 +++++++++++----------------- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e1a754b..f99db40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ license = "Apache-2.0" readme = "README.md" repository = "https://github.com/tisonkun/logforth" rust-version = "1.71.0" -version = "0.5.0" +version = "0.6.0" [package.metadata.docs.rs] all-features = true diff --git a/src/filter/min_level.rs b/src/filter/min_level.rs index 464a3ef..8a8fe19 100644 --- a/src/filter/min_level.rs +++ b/src/filter/min_level.rs @@ -18,10 +18,23 @@ use log::Metadata; use crate::filter::Filter; use crate::filter::FilterResult; +/// A filter that checks if the log level is at most the specified level. +/// +/// From least to most verbose, the levels are: +/// +/// - `Error` +/// - `Warn` +/// - `Info` +/// - `Debug` +/// - `Trace` +/// +/// If MaxLevel is set to `Info`, it will allow `Error`, `Warn`, and `Info` logs. +/// +/// If MaxLevel is set to `Off`, it will reject all logs. #[derive(Debug, Clone)] -pub struct MinLevel(pub LevelFilter); +pub struct MaxLevel(pub LevelFilter); -impl MinLevel { +impl MaxLevel { pub(crate) fn filter(&self, metadata: &Metadata) -> FilterResult { let level = metadata.level(); if level <= self.0 { @@ -31,14 +44,14 @@ impl MinLevel { } } } -impl From for Filter { - fn from(filter: MinLevel) -> Self { - Filter::MinLevel(filter) +impl From for Filter { + fn from(filter: MaxLevel) -> Self { + Filter::MaxLevel(filter) } } impl From for Filter { fn from(filter: LevelFilter) -> Self { - Filter::MinLevel(MinLevel(filter)) + Filter::MaxLevel(MaxLevel(filter)) } } diff --git a/src/filter/mod.rs b/src/filter/mod.rs index a6e2c0e..ff1870c 100644 --- a/src/filter/mod.rs +++ b/src/filter/mod.rs @@ -13,7 +13,7 @@ // limitations under the License. pub use self::custom::CustomFilter; -pub use self::min_level::MinLevel; +pub use self::min_level::MaxLevel; mod custom; mod min_level; @@ -30,14 +30,14 @@ pub enum FilterResult { #[derive(Debug)] pub enum Filter { - MinLevel(MinLevel), + MaxLevel(MaxLevel), Custom(CustomFilter), } impl Filter { pub(crate) fn filter(&self, metadata: &log::Metadata) -> FilterResult { match self { - Filter::MinLevel(filter) => filter.filter(metadata), + Filter::MaxLevel(filter) => filter.filter(metadata), Filter::Custom(filter) => filter.filter(metadata), } } diff --git a/src/logger.rs b/src/logger.rs index c7b7788..34d1129 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -124,26 +124,30 @@ impl Dispatch { /// This struct implements [log::Log] to bridge Logforth's logging implementations /// with the [log] crate. #[derive(Debug)] -pub struct Logger { +pub struct Logger { dispatches: Vec, } -impl Default for Logger { +impl Default for Logger { fn default() -> Self { Self::new() } } -impl Logger { +impl Logger { /// Create a new [Logger] instance. - /// - /// The [Logger] instance is incomplete and must have at least one [Dispatch] added to it. - pub fn new() -> Logger { + pub fn new() -> Logger { Self { dispatches: vec![] } } } -impl Logger { +impl Logger { + /// Add a [Dispatch] to the [Logger]. + pub fn dispatch(mut self, dispatch: Dispatch) -> Logger { + self.dispatches.push(dispatch); + self + } + /// Set up the global logger with the [Logger] instance. /// /// # Errors @@ -156,16 +160,6 @@ impl Logger { } } -impl Logger { - /// Add a [Dispatch] to the [Logger]. - pub fn dispatch(mut self, dispatch: Dispatch) -> Logger { - self.dispatches.push(dispatch); - Logger { - dispatches: self.dispatches, - } - } -} - impl log::Log for Logger { fn enabled(&self, metadata: &Metadata) -> bool { self.dispatches