feat(log): Allow a log formatter per target (#3065)

Co-authored-by: Fabian-Lars <github@fabianlars.de>
This commit is contained in:
Michelle Tilley
2025-11-04 06:52:46 -08:00
committed by GitHub
parent 371cd8227c
commit 2a625adff3
2 changed files with 29 additions and 0 deletions
@@ -0,0 +1,6 @@
---
"log": "minor"
"log-js": "minor"
---
Allow specifying a log formatter per target using the `format` method on `Target`.
+23
View File
@@ -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<Box<Filter>>,
formatter: Option<Box<Formatter>>,
}
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<F>(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<F>(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")]