diff --git a/examples/fn_layout_filter.rs b/examples/fn_layout_filter.rs index b438df5..cd624a5 100644 --- a/examples/fn_layout_filter.rs +++ b/examples/fn_layout_filter.rs @@ -13,31 +13,26 @@ // limitations under the License. use log::LevelFilter; - -use logforth::append::{DispatchAppend, StdoutAppend}; -use logforth::filter::{BoxDynFilter, FilterResult}; -use logforth::layout::BoxDynLayout; -use logforth::logger::Logger; +use logforth::{append, filter, layout}; +use logforth::filter::{FilterResult}; +use logforth::logger::{Dispatch, Logger}; fn main() { - let layout = BoxDynLayout::new(|record: &log::Record| { - let message = format!("[box dyn] {}", record.args()); - Ok(message.into_bytes()) - // ...or - // anyhow::bail!("boom: {}", message) - }); - - let filter = BoxDynFilter::new(|metadata: &log::Metadata| { - if metadata.level() <= LevelFilter::Info { - FilterResult::Accept - } else { - FilterResult::Reject - } - }); - - let append = StdoutAppend::default().with_layout(layout); - let append = DispatchAppend::new(append).filter(filter); - Logger::new().add_append(append).apply().unwrap(); + Logger::new().dispatch(Dispatch::new() + .filter(filter::BoxDyn::new(|metadata: &log::Metadata| { + if metadata.level() <= LevelFilter::Info { + FilterResult::Accept + } else { + FilterResult::Reject + } + })) + .layout(layout::BoxDyn::new(|record: &log::Record| { + let message = format!("[box dyn] {}", record.args()); + Ok(message.into_bytes()) + // ...or + // anyhow::bail!("boom: {}", message) + })) + .append(append::Stdout)); log::error!("Hello error!"); log::warn!("Hello warn!"); diff --git a/examples/json_stdio.rs b/examples/json_stdio.rs index 4d9b2e0..26d98da 100644 --- a/examples/json_stdio.rs +++ b/examples/json_stdio.rs @@ -13,15 +13,17 @@ // limitations under the License. use log::LevelFilter; -use logforth::append::{DispatchAppend, StdoutAppend}; -use logforth::filter::LogLevelFilter; -use logforth::layout::SimpleJsonLayout; -use logforth::logger::Logger; + +use logforth::logger::{Dispatch, Logger}; +use logforth::{append, filter, layout}; fn main() { - let append = StdoutAppend::default().with_layout(SimpleJsonLayout); - let append = DispatchAppend::new(append).filter(LogLevelFilter::new(LevelFilter::Trace)); - Logger::new().add_append(append).apply().unwrap(); + Logger::new().dispatch( + Dispatch::new() + .filter(filter::LogLevel::new(LevelFilter::Trace)) + .layout(layout::SimpleJson) + .append(append::Stdout), + ); log::error!("Hello error!"); log::warn!("Hello warn!"); diff --git a/examples/no_color_stdio.rs b/examples/no_color_stdio.rs index 928cb0b..71e613b 100644 --- a/examples/no_color_stdio.rs +++ b/examples/no_color_stdio.rs @@ -13,14 +13,14 @@ // limitations under the License. use log::LevelFilter; -use logforth::append::{DispatchAppend, StdoutAppend}; -use logforth::filter::LogLevelFilter; -use logforth::logger::Logger; +use logforth::{append, filter, layout}; +use logforth::logger::{Dispatch, Logger}; fn main() { - let append = StdoutAppend::default(); - let append = DispatchAppend::new(append).filter(LogLevelFilter::new(LevelFilter::Trace)); - Logger::new().add_append(append).apply().unwrap(); + Logger::new().dispatch(Dispatch::new() + .filter(filter::LogLevel::new(LevelFilter::Trace)) + .layout(layout::SimpleText::default()) + .append(append::Stdout)); log::error!("Hello error!"); log::warn!("Hello warn!"); diff --git a/examples/rolling_file.rs b/examples/rolling_file.rs index 80c2f97..168fc45 100644 --- a/examples/rolling_file.rs +++ b/examples/rolling_file.rs @@ -13,10 +13,9 @@ // limitations under the License. use log::LevelFilter; -use logforth::append::{DispatchAppend, NonBlockingBuilder, RollingFileAppend, RollingFileWriter, Rotation}; -use logforth::filter::LogLevelFilter; -use logforth::layout::SimpleJsonLayout; -use logforth::logger::Logger; +use logforth::{append, filter, layout}; +use logforth::append::{NonBlockingBuilder, RollingFileWriter, Rotation}; +use logforth::logger::{Dispatch, Logger}; fn main() { let rolling = RollingFileWriter::builder() @@ -28,9 +27,10 @@ fn main() { .unwrap(); let (writer, _guard) = NonBlockingBuilder::default().finish(rolling); - let append = RollingFileAppend::new(writer).with_layout(SimpleJsonLayout); - let append = DispatchAppend::new(append).filter(LogLevelFilter::new(LevelFilter::Trace)); - Logger::new().add_append(append).apply().unwrap(); + Logger::new().dispatch(Dispatch::new() + .filter(filter::LogLevel::new(LevelFilter::Trace)) + .layout(layout::SimpleJson) + .append(append::RollingFile::new(writer))); let repeat = 1; diff --git a/examples/simple_stdio.rs b/examples/simple_stdio.rs index 5999a38..71e613b 100644 --- a/examples/simple_stdio.rs +++ b/examples/simple_stdio.rs @@ -13,15 +13,14 @@ // limitations under the License. use log::LevelFilter; -use logforth::append::{DispatchAppend, StdoutAppend}; -use logforth::filter::LogLevelFilter; -use logforth::layout::SimpleTextLayout; -use logforth::logger::Logger; +use logforth::{append, filter, layout}; +use logforth::logger::{Dispatch, Logger}; fn main() { - let append = StdoutAppend::default().with_layout(SimpleTextLayout::default()); - let append = DispatchAppend::new(append).filter(LogLevelFilter::new(LevelFilter::Trace)); - Logger::new().add_append(append).apply().unwrap(); + Logger::new().dispatch(Dispatch::new() + .filter(filter::LogLevel::new(LevelFilter::Trace)) + .layout(layout::SimpleText::default()) + .append(append::Stdout)); log::error!("Hello error!"); log::warn!("Hello warn!"); diff --git a/src/append/boxdyn.rs b/src/append/boxdyn.rs index 93687a5..90ca29d 100644 --- a/src/append/boxdyn.rs +++ b/src/append/boxdyn.rs @@ -18,21 +18,21 @@ use log::Metadata; use log::Record; use crate::append::{Append, AppendImpl}; -pub struct BoxDynAppend(Box); +pub struct BoxDyn(Box); -impl BoxDynAppend { +impl BoxDyn { pub fn new(append: impl Append + Send + Sync + 'static) -> Self { Self(Box::new(append)) } } -impl Debug for BoxDynAppend { +impl Debug for BoxDyn { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "BoxDynAppend {{ ... }}") } } -impl Append for BoxDynAppend { +impl Append for BoxDyn { fn enabled(&self, metadata: &Metadata) -> bool { (*self.0).enabled(metadata) } @@ -46,8 +46,8 @@ impl Append for BoxDynAppend { } } -impl From for AppendImpl { - fn from(append: BoxDynAppend) -> Self { +impl From for AppendImpl { + fn from(append: BoxDyn) -> Self { AppendImpl::BoxDyn(append) } } diff --git a/src/append/boxlog.rs b/src/append/boxlog.rs index 4bf7a2a..2290ddd 100644 --- a/src/append/boxlog.rs +++ b/src/append/boxlog.rs @@ -19,21 +19,21 @@ use log::Metadata; use log::Record; use crate::append::{Append, AppendImpl}; -pub struct BoxLogAppend(Box); +pub struct BoxLog(Box); -impl Debug for BoxLogAppend { +impl Debug for BoxLog { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "BoxLogAppend {{ ... }}") } } -impl BoxLogAppend { +impl BoxLog { pub fn new(log: impl Log + 'static) -> Self { Self(Box::new(log)) } } -impl Append for BoxLogAppend { +impl Append for BoxLog { fn enabled(&self, metadata: &Metadata) -> bool { (*self.0).enabled(metadata) } @@ -48,8 +48,8 @@ impl Append for BoxLogAppend { } } -impl From for AppendImpl { - fn from(append: BoxLogAppend) -> Self { +impl From for AppendImpl { + fn from(append: BoxLog) -> Self { AppendImpl::BoxLog(append) } } diff --git a/src/append/fastrace.rs b/src/append/fastrace.rs index 50ef56c..5590417 100644 --- a/src/append/fastrace.rs +++ b/src/append/fastrace.rs @@ -20,9 +20,9 @@ use crate::append::{Append, AppendImpl}; use crate::layout::KvDisplay; #[derive(Default, Debug, Clone)] -pub struct FastraceAppend; +pub struct Fastrace; -impl Append for FastraceAppend { +impl Append for Fastrace { fn try_append(&self, record: &Record) -> anyhow::Result<()> { let message = format!( "{} {:>5} {}{}", @@ -36,8 +36,8 @@ impl Append for FastraceAppend { } } -impl From for AppendImpl { - fn from(append: FastraceAppend) -> Self { +impl From for AppendImpl { + fn from(append: Fastrace) -> Self { AppendImpl::Fastrace(append) } } diff --git a/src/append/file/append.rs b/src/append/file/append.rs index 441325b..7b44fa7 100644 --- a/src/append/file/append.rs +++ b/src/append/file/append.rs @@ -18,17 +18,17 @@ use crate::append::file::non_blocking::NonBlocking; use crate::append::{Append, AppendImpl}; #[derive(Debug)] -pub struct RollingFileAppend { +pub struct RollingFile { writer: NonBlocking, } -impl RollingFileAppend { +impl RollingFile { pub fn new(writer: NonBlocking) -> Self { Self { writer } } } -impl Append for RollingFileAppend { +impl Append for RollingFile { fn try_append(&self, record: &Record) -> anyhow::Result<()> { let bytes = format!("{}\n", record.args()).into_bytes(); self.writer.send(bytes)?; @@ -36,8 +36,8 @@ impl Append for RollingFileAppend { } } -impl From for AppendImpl { - fn from(append: RollingFileAppend) -> Self { +impl From for AppendImpl { + fn from(append: RollingFile) -> Self { AppendImpl::RollingFile(append) } } diff --git a/src/append/file/mod.rs b/src/append/file/mod.rs index 3ad6432..a09ae3a 100644 --- a/src/append/file/mod.rs +++ b/src/append/file/mod.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub use append::RollingFileAppend; +pub use append::RollingFile; pub use non_blocking::NonBlocking; pub use non_blocking::NonBlockingBuilder; pub use non_blocking::WorkerGuard; diff --git a/src/append/mod.rs b/src/append/mod.rs index 5712cbb..62cb96c 100644 --- a/src/append/mod.rs +++ b/src/append/mod.rs @@ -19,7 +19,9 @@ pub use fastrace::*; #[cfg(feature = "file")] pub use file::*; pub use stdio::*; -use crate::layout::{IdenticalLayout, LayoutImpl}; + +use crate::layout; +use crate::layout::LayoutImpl; mod boxdyn; mod boxlog; @@ -44,20 +46,20 @@ pub trait Append { /// Default layout to use when [Dispatch][crate::logger::Dispatch] does not configure a /// preferred layout. fn default_layout(&self) -> LayoutImpl { - LayoutImpl::Identical(IdenticalLayout) + LayoutImpl::Identical(layout::Identical) } } #[derive(Debug)] pub enum AppendImpl { - BoxDyn(BoxDynAppend), - BoxLog(BoxLogAppend), + BoxDyn(BoxDyn), + BoxLog(BoxLog), #[cfg(feature = "fastrace")] - Fastrace(FastraceAppend), + Fastrace(Fastrace), #[cfg(feature = "file")] - RollingFile(RollingFileAppend), - Stdout(StdoutAppend), - Stderr(StderrAppend), + RollingFile(RollingFile), + Stdout(Stdout), + Stderr(Stderr), } impl Append for AppendImpl { diff --git a/src/append/stdio.rs b/src/append/stdio.rs index 66eca2e..3baa7f2 100644 --- a/src/append/stdio.rs +++ b/src/append/stdio.rs @@ -17,9 +17,9 @@ use std::io::Write; use crate::append::{Append, AppendImpl}; #[derive(Default, Debug)] -pub struct StdoutAppend; +pub struct Stdout; -impl Append for StdoutAppend { +impl Append for Stdout { fn try_append(&self, record: &log::Record) -> anyhow::Result<()> { let bytes = format!("{}\n", record.args()).into_bytes(); std::io::stdout().write_all(&bytes)?; @@ -31,15 +31,15 @@ impl Append for StdoutAppend { } } -impl From for AppendImpl { - fn from(append: StdoutAppend) -> Self { +impl From for AppendImpl { + fn from(append: Stdout) -> Self { AppendImpl::Stdout(append) } } #[derive(Default, Debug)] -pub struct StderrAppend; -impl Append for StderrAppend { +pub struct Stderr; +impl Append for Stderr { fn try_append(&self, record: &log::Record) -> anyhow::Result<()> { let bytes = format!("{}\n", record.args()).into_bytes(); std::io::stderr().write_all(&bytes)?; @@ -51,8 +51,8 @@ impl Append for StderrAppend { } } -impl From for AppendImpl { - fn from(append: StderrAppend) -> Self { +impl From for AppendImpl { + fn from(append: Stderr) -> Self { AppendImpl::Stderr(append) } } diff --git a/src/filter/boxdyn.rs b/src/filter/boxdyn.rs index 2c1c79e..67168c2 100644 --- a/src/filter/boxdyn.rs +++ b/src/filter/boxdyn.rs @@ -18,21 +18,21 @@ use log::Metadata; use log::Record; use crate::filter::{Filter, FilterImpl, FilterResult}; -pub struct BoxDynFilter(Box); +pub struct BoxDyn(Box); -impl Debug for BoxDynFilter { +impl Debug for BoxDyn { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "BoxDynFilter {{ ... }}") } } -impl BoxDynFilter { +impl BoxDyn { pub fn new(filter: impl Filter + Send + Sync + 'static) -> Self { Self(Box::new(filter)) } } -impl Filter for BoxDynFilter { +impl Filter for BoxDyn { fn filter(&self, record: &Record) -> FilterResult { (*self.0).filter(record) } @@ -42,8 +42,8 @@ impl Filter for BoxDynFilter { } } -impl From for FilterImpl { - fn from(filter: BoxDynFilter) -> Self { +impl From for FilterImpl { + fn from(filter: BoxDyn) -> Self { FilterImpl::BoxDyn(filter) } } diff --git a/src/filter/log_level.rs b/src/filter/log_level.rs index 44ce7fa..c3e2d7a 100644 --- a/src/filter/log_level.rs +++ b/src/filter/log_level.rs @@ -19,17 +19,17 @@ use crate::filter::FilterResult; use crate::filter::{Filter, FilterImpl}; #[derive(Debug, Clone)] -pub struct LogLevelFilter { +pub struct LogLevel { max_level: LevelFilter, } -impl LogLevelFilter { +impl LogLevel { pub fn new(level: LevelFilter) -> Self { Self { max_level: level } } } -impl Filter for LogLevelFilter { +impl Filter for LogLevel { fn filter_metadata(&self, metadata: &Metadata) -> FilterResult { let level = metadata.level(); if level <= self.max_level { @@ -40,8 +40,8 @@ impl Filter for LogLevelFilter { } } -impl From for FilterImpl { - fn from(filter: LogLevelFilter) -> Self { +impl From for FilterImpl { + fn from(filter: LogLevel) -> Self { FilterImpl::LogLevel(filter) } } diff --git a/src/filter/mod.rs b/src/filter/mod.rs index 09a081d..fe4a1e6 100644 --- a/src/filter/mod.rs +++ b/src/filter/mod.rs @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub use boxdyn::BoxDynFilter; -pub use log_level::LogLevelFilter; +pub use boxdyn::BoxDyn; +pub use log_level::LogLevel; mod boxdyn; mod log_level; @@ -38,8 +38,8 @@ pub trait Filter { #[derive(Debug)] pub enum FilterImpl { - BoxDyn(BoxDynFilter), - LogLevel(LogLevelFilter), + BoxDyn(BoxDyn), + LogLevel(LogLevel), } impl Filter for FilterImpl { diff --git a/src/layout/boxdyn.rs b/src/layout/boxdyn.rs index 22e9265..f73ab92 100644 --- a/src/layout/boxdyn.rs +++ b/src/layout/boxdyn.rs @@ -18,34 +18,34 @@ use log::Record; use crate::layout::{Layout, LayoutImpl}; -pub struct BoxDynLayout(Box); +pub struct BoxDyn(Box); -impl Debug for BoxDynLayout { +impl Debug for BoxDyn { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "BoxDynLayout {{ ... }}") } } -impl BoxDynLayout { +impl BoxDyn { pub fn new(layout: impl Layout + Send + Sync + 'static) -> Self { Self(Box::new(layout)) } } -impl Layout for BoxDynLayout { - fn format_record(&self, record: &Record) -> anyhow::Result { +impl Layout for BoxDyn { + fn format_record(&self, record: Record) -> anyhow::Result { (*self.0).format_record(record) } } -impl From for LayoutImpl { - fn from(layout: BoxDynLayout) -> Self { +impl From for LayoutImpl { + fn from(layout: BoxDyn) -> Self { LayoutImpl::BoxDyn(layout) } } -impl anyhow::Result>> Layout for T { - fn format_record(&self, record: &Record) -> anyhow::Result> { +impl Fn(&'a Record<'a>) -> anyhow::Result>> Layout for T { + fn format_record<'a>(&'a self, record: &'a Record) -> anyhow::Result { self(record) } } diff --git a/src/layout/identical.rs b/src/layout/identical.rs index 7359bb3..5e379e0 100644 --- a/src/layout/identical.rs +++ b/src/layout/identical.rs @@ -1,16 +1,18 @@ +use log::Record; + use crate::layout::{Layout, LayoutImpl}; #[derive(Debug, Default, Clone, Copy)] -pub struct IdenticalLayout; +pub struct Identical; -impl Layout for IdenticalLayout { - fn format_record(&self, record: &log::Record) -> anyhow::Result { - Ok(record.clone()) +impl Layout for Identical { + fn format_record(&self, record: Record) -> anyhow::Result { + Ok(record) } } -impl From for LayoutImpl { - fn from(layout: IdenticalLayout) -> Self { +impl From for LayoutImpl { + fn from(layout: Identical) -> Self { LayoutImpl::Identical(layout) } } diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 30d63a7..a2176d1 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -12,12 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub use boxdyn::BoxDynLayout; +pub use boxdyn::BoxDyn; +pub use identical::Identical; pub use kv_display::KvDisplay; #[cfg(feature = "json")] -pub use simple_json::SimpleJsonLayout; -pub use simple_text::SimpleTextLayout; -pub use identical::IdenticalLayout; +pub use simple_json::SimpleJson; +pub use simple_text::SimpleText; mod boxdyn; mod identical; @@ -27,26 +27,20 @@ mod simple_json; mod simple_text; pub trait Layout { - fn format_record(&self, record: &log::Record) -> anyhow::Result; + fn format_record(&self, record: log::Record) -> anyhow::Result; } #[derive(Debug)] pub enum LayoutImpl { - BoxDyn(BoxDynLayout), - Identical(IdenticalLayout), - SimpleText(SimpleTextLayout), + BoxDyn(BoxDyn), + Identical(Identical), + SimpleText(SimpleText), #[cfg(feature = "json")] - SimpleJson(SimpleJsonLayout), -} - -impl Default for LayoutImpl { - fn default() -> Self { - LayoutImpl::SimpleText(SimpleTextLayout::default()) - } + SimpleJson(SimpleJson), } impl Layout for LayoutImpl { - fn format_record(&self, record: &log::Record) -> anyhow::Result { + fn format_record(&self, record: log::Record) -> anyhow::Result { match self { LayoutImpl::BoxDyn(layout) => layout.format_record(record), LayoutImpl::Identical(layout) => layout.format_record(record), diff --git a/src/layout/simple_json.rs b/src/layout/simple_json.rs index deb5ad1..47dbc55 100644 --- a/src/layout/simple_json.rs +++ b/src/layout/simple_json.rs @@ -24,7 +24,7 @@ use serde_json::Value; use crate::layout::{Layout, LayoutImpl}; #[derive(Default, Debug, Clone)] -pub struct SimpleJsonLayout; +pub struct SimpleJson; struct KvCollector<'a> { kvs: &'a mut Map, @@ -54,8 +54,8 @@ struct RecordLine<'a> { kvs: Map, } -impl Layout for SimpleJsonLayout { - fn format_record(&self, record: &Record) -> anyhow::Result { +impl Layout for SimpleJson { + fn format_record(&self, record: Record) -> anyhow::Result { let mut kvs = Map::new(); let mut visitor = KvCollector { kvs: &mut kvs }; record.key_values().visit(&mut visitor)?; @@ -80,8 +80,8 @@ impl Layout for SimpleJsonLayout { } } -impl From for LayoutImpl { - fn from(layout: SimpleJsonLayout) -> Self { +impl From for LayoutImpl { + fn from(layout: SimpleJson) -> Self { LayoutImpl::SimpleJson(layout) } } diff --git a/src/layout/simple_text.rs b/src/layout/simple_text.rs index 6711f15..4523d30 100644 --- a/src/layout/simple_text.rs +++ b/src/layout/simple_text.rs @@ -25,7 +25,7 @@ use crate::layout::{Layout, LayoutImpl}; use crate::layout::kv_display::KvDisplay; #[derive(Default, Debug, Clone)] -pub struct SimpleTextLayout { +pub struct SimpleText { pub colors: ColoredLevel, } @@ -50,8 +50,8 @@ impl Default for ColoredLevel { } } -impl Layout for SimpleTextLayout { - fn format_record(&self, record: &Record) -> anyhow::Result { +impl Layout for SimpleText { + fn format_record(&self, record: Record) -> anyhow::Result { let color = match record.level() { Level::Error => self.colors.error, Level::Warn => self.colors.warn, @@ -81,8 +81,8 @@ impl Layout for SimpleTextLayout { } } -impl From for LayoutImpl { - fn from(layout: SimpleTextLayout) -> Self { +impl From for LayoutImpl { + fn from(layout: SimpleText) -> Self { LayoutImpl::SimpleText(layout) } } diff --git a/src/logger.rs b/src/logger.rs index 8eab99b..82e2683 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -39,18 +39,18 @@ impl Dispatch { } } - pub fn filter(mut self, filter: FilterImpl) -> Self { - self.filters.push(filter); + pub fn filter(mut self, filter: impl Into) -> Self { + self.filters.push(filter.into()); self } - pub fn append(mut self, append: AppendImpl) -> Self { - self.appends.push(append); + pub fn append(mut self, append: impl Into) -> Self { + self.appends.push(append.into()); self } - pub fn layout(mut self, layout: LayoutImpl) -> Self { - self.preferred_layout = Some(layout); + pub fn layout(mut self, layout: impl Into) -> Self { + self.preferred_layout = Some(layout.into()); self } @@ -80,15 +80,13 @@ impl Dispatch { } } + let record = record.clone(); for append in &self.appends { - let layout = self - .preferred_layout - .as_ref() - .unwrap_or(&append.default_layout()); - - layout - .format_record(record) - .and_then(|formatted| append.try_append(&formatted))?; + let record = match self.preferred_layout.as_ref() { + Some(layout) => layout.format_record(record)?, + None => append.default_layout().format_record(record)?, + }; + append.try_append(&record)?; } Ok(()) }