mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-27 11:56:05 +02:00
feat(log): Allow a log formatter per target (#3065)
Co-authored-by: Fabian-Lars <github@fabianlars.de>
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"log": "minor"
|
||||
"log-js": "minor"
|
||||
---
|
||||
|
||||
Allow specifying a log formatter per target using the `format` method on `Target`.
|
||||
@@ -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")]
|
||||
|
||||
Reference in New Issue
Block a user