diff --git a/examples/fn_layout_filter.rs b/examples/fn_layout_filter.rs index a35d4ea..08dd4c3 100644 --- a/examples/fn_layout_filter.rs +++ b/examples/fn_layout_filter.rs @@ -21,23 +21,26 @@ use logforth::logger::Dispatch; use logforth::logger::Logger; fn main() { - 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), - ); + 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 args = format_args!("[box dyn] {}", record.args()); + // Ok(record.to_builder().args(args).build()) + // // ...or + // // anyhow::bail!("boom: {}", message) + // })) + .append(append::Stdout), + ) + .apply() + .unwrap(); log::error!("Hello error!"); log::warn!("Hello warn!"); diff --git a/examples/json_stdio.rs b/examples/json_stdio.rs index e5d744f..7951297 100644 --- a/examples/json_stdio.rs +++ b/examples/json_stdio.rs @@ -20,12 +20,15 @@ use logforth::logger::Dispatch; use logforth::logger::Logger; fn main() { - Logger::new().dispatch( - Dispatch::new() - .filter(filter::LogLevel::new(LevelFilter::Trace)) - .layout(layout::SimpleJson) - .append(append::Stdout), - ); + Logger::new() + .dispatch( + Dispatch::new() + .filter(filter::LogLevel::new(LevelFilter::Trace)) + .layout(layout::SimpleJson) + .append(append::Stdout), + ) + .apply() + .unwrap(); log::error!("Hello error!"); log::warn!("Hello warn!"); diff --git a/examples/no_color_stdio.rs b/examples/no_color_stdio.rs index 3744286..24e411c 100644 --- a/examples/no_color_stdio.rs +++ b/examples/no_color_stdio.rs @@ -20,12 +20,15 @@ use logforth::logger::Dispatch; use logforth::logger::Logger; fn main() { - Logger::new().dispatch( - Dispatch::new() - .filter(filter::LogLevel::new(LevelFilter::Trace)) - .layout(layout::SimpleText::default()) - .append(append::Stdout), - ); + Logger::new() + .dispatch( + Dispatch::new() + .filter(filter::LogLevel::new(LevelFilter::Trace)) + .layout(layout::SimpleText::default()) + .append(append::Stdout), + ) + .apply() + .unwrap(); log::error!("Hello error!"); log::warn!("Hello warn!"); diff --git a/examples/rolling_file.rs b/examples/rolling_file.rs index cb4ce6f..0e19f09 100644 --- a/examples/rolling_file.rs +++ b/examples/rolling_file.rs @@ -32,12 +32,15 @@ fn main() { .unwrap(); let (writer, _guard) = NonBlockingBuilder::default().finish(rolling); - Logger::new().dispatch( - Dispatch::new() - .filter(filter::LogLevel::new(LevelFilter::Trace)) - .layout(layout::SimpleJson) - .append(append::RollingFile::new(writer)), - ); + Logger::new() + .dispatch( + Dispatch::new() + .filter(filter::LogLevel::new(LevelFilter::Trace)) + .layout(layout::SimpleJson) + .append(append::RollingFile::new(writer)), + ) + .apply() + .unwrap(); let repeat = 1; diff --git a/examples/simple_stdio.rs b/examples/simple_stdio.rs index 3744286..24e411c 100644 --- a/examples/simple_stdio.rs +++ b/examples/simple_stdio.rs @@ -20,12 +20,15 @@ use logforth::logger::Dispatch; use logforth::logger::Logger; fn main() { - Logger::new().dispatch( - Dispatch::new() - .filter(filter::LogLevel::new(LevelFilter::Trace)) - .layout(layout::SimpleText::default()) - .append(append::Stdout), - ); + Logger::new() + .dispatch( + Dispatch::new() + .filter(filter::LogLevel::new(LevelFilter::Trace)) + .layout(layout::SimpleText::default()) + .append(append::Stdout), + ) + .apply() + .unwrap(); log::error!("Hello error!"); log::warn!("Hello warn!"); diff --git a/src/layout/boxdyn.rs b/src/layout/boxdyn.rs index d47214e..d09353c 100644 --- a/src/layout/boxdyn.rs +++ b/src/layout/boxdyn.rs @@ -34,7 +34,7 @@ impl BoxDyn { } impl Layout for BoxDyn { - fn format_record(&self, record: Record) -> anyhow::Result { + fn format_record<'a>(&'_ self, record: &'a Record<'a>) -> anyhow::Result> { (*self.0).format_record(record) } } @@ -45,8 +45,8 @@ impl From for LayoutImpl { } } -impl Fn(&'a Record<'a>) -> anyhow::Result>> Layout for T { - fn format_record<'a>(&'a self, record: &'a Record) -> anyhow::Result { +impl Fn(&Record<'a>) -> anyhow::Result>> Layout for T { + fn format_record<'a>(&'_ self, record: &'a Record<'a>) -> anyhow::Result> { self(record) } } diff --git a/src/layout/identical.rs b/src/layout/identical.rs index 1d10fc2..422a9dd 100644 --- a/src/layout/identical.rs +++ b/src/layout/identical.rs @@ -7,8 +7,8 @@ use crate::layout::LayoutImpl; pub struct Identical; impl Layout for Identical { - fn format_record(&self, record: Record) -> anyhow::Result { - Ok(record) + fn format_record<'a>(&'_ self, record: &'a Record<'a>) -> anyhow::Result> { + Ok(record.clone()) } } diff --git a/src/layout/mod.rs b/src/layout/mod.rs index a2176d1..4910b5e 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -27,7 +27,7 @@ mod simple_json; mod simple_text; pub trait Layout { - fn format_record(&self, record: log::Record) -> anyhow::Result; + fn format_record<'a>(&'_ self, record: &'a log::Record<'a>) -> anyhow::Result>; } #[derive(Debug)] @@ -40,7 +40,7 @@ pub enum LayoutImpl { } impl Layout for LayoutImpl { - fn format_record(&self, record: log::Record) -> anyhow::Result { + fn format_record<'a>(&'_ self, record: &'a log::Record<'a>) -> 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 d53310b..55ef53b 100644 --- a/src/layout/simple_json.rs +++ b/src/layout/simple_json.rs @@ -56,7 +56,7 @@ struct RecordLine<'a> { } impl Layout for SimpleJson { - fn format_record(&self, record: Record) -> anyhow::Result { + fn format_record<'a>(&'_ self, record: &'a Record<'a>) -> anyhow::Result> { let mut kvs = Map::new(); let mut visitor = KvCollector { kvs: &mut kvs }; record.key_values().visit(&mut visitor)?; diff --git a/src/layout/simple_text.rs b/src/layout/simple_text.rs index aadfa7f..e094af8 100644 --- a/src/layout/simple_text.rs +++ b/src/layout/simple_text.rs @@ -52,7 +52,7 @@ impl Default for ColoredLevel { } impl Layout for SimpleText { - fn format_record(&self, record: Record) -> anyhow::Result { + fn format_record<'a>(&'_ self, record: &'a Record<'a>) -> anyhow::Result> { let color = match record.level() { Level::Error => self.colors.error, Level::Warn => self.colors.warn, @@ -60,13 +60,11 @@ impl Layout for SimpleText { Level::Debug => self.colors.debug, Level::Trace => self.colors.trace, }; - let record_level = record.level().to_string(); - let record_level = ColoredString::from(record_level).color(color); let args = format_args!( "{} {:>5} {}: {}:{} {}{}", humantime::format_rfc3339_micros(SystemTime::now()), - record_level, + ColoredString::from(record.level().to_string()).color(color), record.module_path().unwrap_or(""), record .file() diff --git a/src/logger.rs b/src/logger.rs index 56efe13..c2c0fdd 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -83,7 +83,6 @@ impl Dispatch { } } - let record = record.clone(); for append in &self.appends { let record = match self.preferred_layout.as_ref() { Some(layout) => layout.format_record(record)?,