Skip to content

Commit

Permalink
fixup lifetime
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Aug 1, 2024
1 parent e0963f2 commit 30d72a0
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 54 deletions.
37 changes: 20 additions & 17 deletions examples/fn_layout_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
15 changes: 9 additions & 6 deletions examples/json_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
15 changes: 9 additions & 6 deletions examples/no_color_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
15 changes: 9 additions & 6 deletions examples/rolling_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 9 additions & 6 deletions examples/simple_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
6 changes: 3 additions & 3 deletions src/layout/boxdyn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl BoxDyn {
}

impl Layout for BoxDyn {
fn format_record(&self, record: Record) -> anyhow::Result<Record> {
fn format_record<'a>(&'_ self, record: &'a Record<'a>) -> anyhow::Result<Record<'a>> {
(*self.0).format_record(record)
}
}
Expand All @@ -45,8 +45,8 @@ impl From<BoxDyn> for LayoutImpl {
}
}

impl<T: for<'a> Fn(&'a Record<'a>) -> anyhow::Result<Record<'a>>> Layout for T {
fn format_record<'a>(&'a self, record: &'a Record) -> anyhow::Result<Record> {
impl<T: for<'a> Fn(&Record<'a>) -> anyhow::Result<Record<'a>>> Layout for T {
fn format_record<'a>(&'_ self, record: &'a Record<'a>) -> anyhow::Result<Record<'a>> {
self(record)
}
}
4 changes: 2 additions & 2 deletions src/layout/identical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::layout::LayoutImpl;
pub struct Identical;

impl Layout for Identical {
fn format_record(&self, record: Record) -> anyhow::Result<Record> {
Ok(record)
fn format_record<'a>(&'_ self, record: &'a Record<'a>) -> anyhow::Result<Record<'a>> {
Ok(record.clone())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod simple_json;
mod simple_text;

pub trait Layout {
fn format_record(&self, record: log::Record) -> anyhow::Result<log::Record>;
fn format_record<'a>(&'_ self, record: &'a log::Record<'a>) -> anyhow::Result<log::Record<'a>>;
}

#[derive(Debug)]
Expand All @@ -40,7 +40,7 @@ pub enum LayoutImpl {
}

impl Layout for LayoutImpl {
fn format_record(&self, record: log::Record) -> anyhow::Result<log::Record> {
fn format_record<'a>(&'_ self, record: &'a log::Record<'a>) -> anyhow::Result<log::Record<'a>> {
match self {
LayoutImpl::BoxDyn(layout) => layout.format_record(record),
LayoutImpl::Identical(layout) => layout.format_record(record),
Expand Down
2 changes: 1 addition & 1 deletion src/layout/simple_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct RecordLine<'a> {
}

impl Layout for SimpleJson {
fn format_record(&self, record: Record) -> anyhow::Result<Record> {
fn format_record<'a>(&'_ self, record: &'a Record<'a>) -> anyhow::Result<Record<'a>> {
let mut kvs = Map::new();
let mut visitor = KvCollector { kvs: &mut kvs };
record.key_values().visit(&mut visitor)?;
Expand Down
6 changes: 2 additions & 4 deletions src/layout/simple_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,19 @@ impl Default for ColoredLevel {
}

impl Layout for SimpleText {
fn format_record(&self, record: Record) -> anyhow::Result<Record> {
fn format_record<'a>(&'_ self, record: &'a Record<'a>) -> anyhow::Result<Record<'a>> {
let color = match record.level() {
Level::Error => self.colors.error,
Level::Warn => self.colors.warn,
Level::Info => self.colors.info,
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()
Expand Down
1 change: 0 additions & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?,
Expand Down

0 comments on commit 30d72a0

Please sign in to comment.