diff --git a/.changes/allow-log-formatter-per-target.md b/.changes/allow-log-formatter-per-target.md new file mode 100644 index 0000000000..f92f4d2623 --- /dev/null +++ b/.changes/allow-log-formatter-per-target.md @@ -0,0 +1,6 @@ +--- +"log": "minor" +"log-js": "minor" +--- + +Allow specifying a log formatter per target using the `format` method on `Target`. diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index c0642d4178..89e092b86d 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -182,10 +182,13 @@ pub enum TargetKind { Dispatch(fern::Dispatch), } +type Formatter = dyn Fn(FormatCallback, &Arguments, &Record) + Send + Sync + 'static; + /// A log target. pub struct Target { kind: TargetKind, filters: Vec>, + formatter: Option>, } impl Target { @@ -194,6 +197,7 @@ impl Target { Self { kind, filters: Vec::new(), + formatter: None, } } @@ -205,6 +209,15 @@ impl Target { self.filters.push(Box::new(filter)); self } + + #[inline] + pub fn format(mut self, formatter: F) -> Self + where + F: Fn(FormatCallback, &Arguments, &Record) + Send + Sync + 'static, + { + self.formatter.replace(Box::new(formatter)); + self + } } pub struct Builder { @@ -276,6 +289,13 @@ impl Builder { self } + pub fn clear_format(mut self) -> Self { + self.dispatch = self.dispatch.format(|out, message, _record| { + out.finish(format_args!("{message}")); + }); + self + } + pub fn format(mut self, formatter: F) -> Self where F: Fn(FormatCallback, &Arguments, &Record) + Sync + Send + 'static, @@ -384,6 +404,9 @@ impl Builder { for filter in target.filters { target_dispatch = target_dispatch.filter(filter); } + if let Some(formatter) = target.formatter { + target_dispatch = target_dispatch.format(formatter); + } let logger = match target.kind { #[cfg(target_os = "android")]