From 44afe704a0293cf2ff117afde199ecdae07ee164 Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 1 Aug 2024 22:37:25 +0800 Subject: [PATCH] feat: typed DispatchBuilder (#16) Signed-off-by: tison --- Cargo.toml | 2 +- examples/fn_layout_filter.rs | 5 +-- examples/json_stdio.rs | 5 +-- examples/no_color_stdio.rs | 5 +-- examples/rolling_file.rs | 5 +-- examples/simple_stdio.rs | 5 +-- src/append/opentelemetry.rs | 8 +++- src/logger.rs | 79 ++++++++++++++++++++++++++++-------- 8 files changed, 80 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 32bbb81..aafb7be 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.3.0" +version = "0.4.0" [package.metadata.docs.rs] all-features = true diff --git a/examples/fn_layout_filter.rs b/examples/fn_layout_filter.rs index f8414d6..b4a1cad 100644 --- a/examples/fn_layout_filter.rs +++ b/examples/fn_layout_filter.rs @@ -23,7 +23,7 @@ use logforth::logger::Logger; fn main() { Logger::new() .dispatch( - Dispatch::new() + Dispatch::builder(append::Stdout) .filter(filter::BoxDyn::new(|metadata: &log::Metadata| { if metadata.level() > LevelFilter::Info { FilterResult::Accept @@ -33,8 +33,7 @@ fn main() { })) .layout(layout::CustomLayout::new(|record, f| { f(format_args!("[system alert] {}", record.args())) - })) - .append(append::Stdout), + })), ) .apply() .unwrap(); diff --git a/examples/json_stdio.rs b/examples/json_stdio.rs index 7951297..a69b50f 100644 --- a/examples/json_stdio.rs +++ b/examples/json_stdio.rs @@ -22,10 +22,9 @@ use logforth::logger::Logger; fn main() { Logger::new() .dispatch( - Dispatch::new() + Dispatch::builder(append::Stdout) .filter(filter::LogLevel::new(LevelFilter::Trace)) - .layout(layout::SimpleJson) - .append(append::Stdout), + .layout(layout::SimpleJson), ) .apply() .unwrap(); diff --git a/examples/no_color_stdio.rs b/examples/no_color_stdio.rs index 24e411c..f09ec71 100644 --- a/examples/no_color_stdio.rs +++ b/examples/no_color_stdio.rs @@ -22,10 +22,9 @@ use logforth::logger::Logger; fn main() { Logger::new() .dispatch( - Dispatch::new() + Dispatch::builder(append::Stdout) .filter(filter::LogLevel::new(LevelFilter::Trace)) - .layout(layout::SimpleText::default()) - .append(append::Stdout), + .layout(layout::SimpleText::default()), ) .apply() .unwrap(); diff --git a/examples/rolling_file.rs b/examples/rolling_file.rs index 0e19f09..e433b05 100644 --- a/examples/rolling_file.rs +++ b/examples/rolling_file.rs @@ -34,10 +34,9 @@ fn main() { Logger::new() .dispatch( - Dispatch::new() + Dispatch::builder(append::RollingFile::new(writer)) .filter(filter::LogLevel::new(LevelFilter::Trace)) - .layout(layout::SimpleJson) - .append(append::RollingFile::new(writer)), + .layout(layout::SimpleJson), ) .apply() .unwrap(); diff --git a/examples/simple_stdio.rs b/examples/simple_stdio.rs index 24e411c..f09ec71 100644 --- a/examples/simple_stdio.rs +++ b/examples/simple_stdio.rs @@ -22,10 +22,9 @@ use logforth::logger::Logger; fn main() { Logger::new() .dispatch( - Dispatch::new() + Dispatch::builder(append::Stdout) .filter(filter::LogLevel::new(LevelFilter::Trace)) - .layout(layout::SimpleText::default()) - .append(append::Stdout), + .layout(layout::SimpleText::default()), ) .apply() .unwrap(); diff --git a/src/append/opentelemetry.rs b/src/append/opentelemetry.rs index cf3eb8f..7bca949 100644 --- a/src/append/opentelemetry.rs +++ b/src/append/opentelemetry.rs @@ -23,7 +23,7 @@ use opentelemetry::InstrumentationLibrary; use opentelemetry_otlp::WithExportConfig; use opentelemetry_sdk::logs::LoggerProvider; -use crate::append::Append; +use crate::append::{Append, AppendImpl}; #[derive(Debug)] pub struct OpenTelemetryLog { @@ -107,3 +107,9 @@ fn log_level_to_otel_severity(level: log::Level) -> Severity { log::Level::Trace => Severity::Trace, } } + +impl From for AppendImpl { + fn from(append: OpenTelemetryLog) -> Self { + AppendImpl::OpenTelemetryLog(append) + } +} diff --git a/src/logger.rs b/src/logger.rs index 44fa739..acea6a2 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -26,40 +26,85 @@ use crate::filter::FilterResult; use crate::layout::Layout; #[derive(Debug)] -pub struct Dispatch { - filters: Vec, +pub struct DispatchBuilder { appends: Vec, - preferred_layout: Option, } -impl Default for Dispatch { - fn default() -> Self { - Self::new() +impl DispatchBuilder { + pub fn new(append: impl Into) -> Self { + Self { + appends: vec![append.into()], + } } -} -impl Dispatch { - pub fn new() -> Self { - Self { + pub fn append(mut self, append: impl Into) -> Self { + self.appends.push(append.into()); + self + } + + pub fn filter(self, filter: impl Into) -> DispatchFilterBuilder { + DispatchFilterBuilder { + appends: self.appends, + filters: vec![filter.into()], + } + } + + pub fn layout(self, layout: impl Into) -> Dispatch { + Dispatch { + filters: vec![], + appends: self.appends, + preferred_layout: Some(layout.into()), + } + } + + pub fn finish(self) -> Dispatch { + Dispatch { filters: vec![], - appends: vec![], + appends: self.appends, preferred_layout: None, } } +} +#[derive(Debug)] +pub struct DispatchFilterBuilder { + appends: Vec, + filters: Vec, +} + +impl DispatchFilterBuilder { pub fn filter(mut self, filter: impl Into) -> Self { self.filters.push(filter.into()); self } - pub fn append(mut self, append: impl Into) -> Self { - self.appends.push(append.into()); - self + pub fn layout(self, layout: impl Into) -> Dispatch { + Dispatch { + filters: self.filters, + appends: self.appends, + preferred_layout: Some(layout.into()), + } } - pub fn layout(mut self, layout: impl Into) -> Self { - self.preferred_layout = Some(layout.into()); - self + pub fn finish(self) -> Dispatch { + Dispatch { + filters: self.filters, + appends: self.appends, + preferred_layout: None, + } + } +} + +#[derive(Debug)] +pub struct Dispatch { + filters: Vec, + appends: Vec, + preferred_layout: Option, +} + +impl Dispatch { + pub fn builder(append: impl Into) -> DispatchBuilder { + DispatchBuilder::new(append) } fn enabled(&self, metadata: &Metadata) -> bool {