From 7046423230b2191aecb2976a39ba052c5725156b Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 2 Aug 2024 18:27:27 +0800 Subject: [PATCH] chore: tidy some code randomly (#26) Signed-off-by: tison --- src/filter/custom.rs | 5 ++--- src/layout/custom.rs | 23 +++++++++++++---------- src/layout/json.rs | 42 ++++++++++++++++++++++++++---------------- src/layout/text.rs | 9 ++------- 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/filter/custom.rs b/src/filter/custom.rs index 0553e7f..a5cdb70 100644 --- a/src/filter/custom.rs +++ b/src/filter/custom.rs @@ -20,8 +20,7 @@ use crate::filter::Filter; use crate::filter::FilterResult; pub struct CustomFilter { - #[allow(clippy::type_complexity)] - f: Box FilterResult + Send + Sync + 'static>, + f: Box FilterResult + Send + Sync + 'static>, } impl Debug for CustomFilter { @@ -31,7 +30,7 @@ impl Debug for CustomFilter { } impl CustomFilter { - pub fn new(filter: impl Fn(&log::Metadata) -> FilterResult + Send + Sync + 'static) -> Self { + pub fn new(filter: impl Fn(&Metadata) -> FilterResult + Send + Sync + 'static) -> Self { CustomFilter { f: Box::new(filter), } diff --git a/src/layout/custom.rs b/src/layout/custom.rs index ad177c0..a5a149c 100644 --- a/src/layout/custom.rs +++ b/src/layout/custom.rs @@ -16,27 +16,30 @@ use std::fmt::Arguments; use std::fmt::Debug; use std::fmt::Formatter; +use log::Record; + use crate::layout::Layout; +// TODO(tisonkun): use trait alias when it's stable - https://github.com/rust-lang/rust/issues/41517 +// then we can use the alias for both `dyn` and `impl`. +type FormatFunction = dyn Fn(&Record, &dyn Fn(Arguments) -> anyhow::Result<()>) -> anyhow::Result<()> + + Send + + Sync + + 'static; + pub struct CustomLayout { - #[allow(clippy::type_complexity)] - f: Box< - dyn Fn(&log::Record, &dyn Fn(Arguments) -> anyhow::Result<()>) -> anyhow::Result<()> - + Send - + Sync - + 'static, - >, + f: Box, } impl Debug for CustomLayout { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { write!(f, "CustomLayout {{ ... }}") } } impl CustomLayout { pub fn new( - layout: impl Fn(&log::Record, &dyn Fn(Arguments) -> anyhow::Result<()>) -> anyhow::Result<()> + layout: impl Fn(&Record, &dyn Fn(Arguments) -> anyhow::Result<()>) -> anyhow::Result<()> + Send + Sync + 'static, @@ -46,7 +49,7 @@ impl CustomLayout { } } - pub(crate) fn format(&self, record: &log::Record, f: &F) -> anyhow::Result<()> + pub(crate) fn format(&self, record: &Record, f: &F) -> anyhow::Result<()> where F: Fn(Arguments) -> anyhow::Result<()>, { diff --git a/src/layout/json.rs b/src/layout/json.rs index df52625..30d716b 100644 --- a/src/layout/json.rs +++ b/src/layout/json.rs @@ -13,11 +13,10 @@ // limitations under the License. use std::fmt::Arguments; -use std::path::Path; use std::time::SystemTime; +use humantime::Rfc3339Timestamp; use log::Record; -use serde::Deserialize; use serde::Serialize; use serde_json::Map; use serde_json::Value; @@ -44,17 +43,33 @@ impl<'a, 'kvs> log::kv::Visitor<'kvs> for KvCollector<'a> { } } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize)] struct RecordLine<'a> { - timestamp: String, - level: String, + #[serde(serialize_with = "serialize_timestamp")] + timestamp: Rfc3339Timestamp, + level: &'a str, module_path: &'a str, file: &'a str, line: u32, - message: String, + #[serde(serialize_with = "serialize_args")] + message: &'a Arguments<'a>, kvs: Map, } +fn serialize_args(args: &Arguments, serializer: S) -> Result +where + S: serde::Serializer, +{ + serializer.collect_str(args) +} + +fn serialize_timestamp(timestamp: &Rfc3339Timestamp, serializer: S) -> Result +where + S: serde::Serializer, +{ + serializer.collect_str(&format_args!("{timestamp}")) +} + impl JsonLayout { pub(crate) fn format(&self, record: &Record, f: &F) -> anyhow::Result<()> where @@ -64,18 +79,13 @@ impl JsonLayout { let mut visitor = KvCollector { kvs: &mut kvs }; record.key_values().visit(&mut visitor)?; - let timestamp = humantime::format_rfc3339_micros(SystemTime::now()); let record_line = RecordLine { - timestamp: format!("{timestamp}"), - level: record.level().to_string(), - module_path: record.module_path().unwrap_or(""), - file: record - .file() - .and_then(|file| Path::new(file).file_name()) - .and_then(|name| name.to_str()) - .unwrap_or_default(), + timestamp: humantime::format_rfc3339_micros(SystemTime::now()), + level: record.level().as_str(), + module_path: record.module_path().unwrap_or_default(), + file: record.file().unwrap_or_default(), line: record.line().unwrap_or(0), - message: record.args().to_string(), + message: record.args(), kvs, }; diff --git a/src/layout/text.rs b/src/layout/text.rs index a0691e8..5fcc57a 100644 --- a/src/layout/text.rs +++ b/src/layout/text.rs @@ -13,7 +13,6 @@ // limitations under the License. use std::fmt::Arguments; -use std::path::Path; use std::time::SystemTime; use colored::Color; @@ -65,12 +64,8 @@ impl TextLayout { let time = humantime::format_rfc3339_micros(SystemTime::now()); let level = ColoredString::from(record.level().to_string()).color(color); - let module = record.module_path().unwrap_or(""); - let file = record - .file() - .and_then(|file| Path::new(file).file_name()) - .and_then(|name| name.to_str()) - .unwrap_or_default(); + let module = record.module_path().unwrap_or_default(); + let file = record.file().unwrap_or_default(); let line = record.line().unwrap_or(0); let message = record.args(); let kvs = KvDisplay::new(record.key_values());