rust_loguru/formatters/
mod.rs

1pub mod json;
2pub mod template;
3pub mod text;
4pub mod util;
5
6use crate::record::Record;
7use std::fmt::Debug;
8use std::sync::Arc;
9
10/// A type alias for a format function
11pub type FormatFn = Arc<dyn Fn(&Record) -> String + Send + Sync>;
12
13/// A trait for formatters that format log records
14pub trait FormatterTrait: Send + Sync + Debug {
15    /// Format a single record into a string
16    fn fmt(&self, record: &Record) -> String;
17
18    /// Format multiple records into a string (default implementation)
19    fn fmt_batch(&self, records: &[Record]) -> String {
20        records
21            .iter()
22            .map(|record| FormatterTrait::fmt(self, record))
23            .collect::<Vec<_>>()
24            .join("\n")
25    }
26
27    /// Enable or disable colored output
28    fn with_colors(&mut self, use_colors: bool);
29
30    /// Enable or disable timestamp output
31    fn with_timestamp(&mut self, include_timestamp: bool);
32
33    /// Enable or disable level output
34    fn with_level(&mut self, include_level: bool);
35
36    /// Enable or disable module output
37    fn with_module(&mut self, include_module: bool);
38
39    /// Enable or disable location output
40    fn with_location(&mut self, include_location: bool);
41
42    /// Set the pattern for formatting
43    fn with_pattern(&mut self, pattern: String);
44
45    /// Set a custom format function
46    fn with_format(&mut self, format_fn: FormatFn);
47
48    /// Clone the formatter into a boxed trait object
49    fn box_clone(&self) -> Box<dyn FormatterTrait + Send + Sync>;
50}
51
52/// A formatter that can format log records
53#[derive(Debug)]
54pub struct Formatter {
55    inner: Box<dyn FormatterTrait + Send + Sync>,
56}
57
58impl Clone for Formatter {
59    fn clone(&self) -> Self {
60        Self {
61            inner: self.inner.box_clone(),
62        }
63    }
64}
65
66impl Default for Formatter {
67    fn default() -> Self {
68        Self::text()
69    }
70}
71
72impl Formatter {
73    /// Create a new text formatter
74    pub fn text() -> Self {
75        Self {
76            inner: Box::new(crate::formatters::text::TextFormatter::default()),
77        }
78    }
79
80    /// Create a new JSON formatter
81    pub fn json() -> Self {
82        Self {
83            inner: Box::new(crate::formatters::json::JsonFormatter::default()),
84        }
85    }
86
87    /// Create a new template formatter
88    pub fn template(template: impl Into<String>) -> Self {
89        Self {
90            inner: Box::new(crate::formatters::template::TemplateFormatter::new(
91                template,
92            )),
93        }
94    }
95
96    /// Format a single record into a string
97    pub fn format(&self, record: &Record) -> String {
98        FormatterTrait::fmt(&*self.inner, record)
99    }
100
101    /// Format multiple records into a string
102    pub fn format_batch(&self, records: &[Record]) -> String {
103        self.inner.fmt_batch(records)
104    }
105
106    /// Enable or disable colored output
107    pub fn with_colors(mut self, use_colors: bool) -> Self {
108        self.inner.with_colors(use_colors);
109        self
110    }
111
112    /// Enable or disable timestamp output
113    pub fn with_timestamp(mut self, include_timestamp: bool) -> Self {
114        self.inner.with_timestamp(include_timestamp);
115        self
116    }
117
118    /// Enable or disable level output
119    pub fn with_level(mut self, include_level: bool) -> Self {
120        self.inner.with_level(include_level);
121        self
122    }
123
124    /// Enable or disable module output
125    pub fn with_module(mut self, include_module: bool) -> Self {
126        self.inner.with_module(include_module);
127        self
128    }
129
130    /// Enable or disable location output
131    pub fn with_location(mut self, include_location: bool) -> Self {
132        self.inner.with_location(include_location);
133        self
134    }
135
136    /// Set the pattern for formatting
137    pub fn with_pattern(mut self, pattern: impl Into<String>) -> Self {
138        self.inner.with_pattern(pattern.into());
139        self
140    }
141
142    /// Set a custom format function
143    pub fn with_format<F>(mut self, format_fn: F) -> Self
144    where
145        F: Fn(&Record) -> String + Send + Sync + 'static,
146    {
147        self.inner.with_format(Arc::new(format_fn));
148        self
149    }
150}
151
152// Re-export formatters
153pub use self::json::JsonFormatter;
154pub use self::template::TemplateFormatter;
155pub use self::text::TextFormatter;