rust_loguru/formatters/
mod.rs1pub 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
10pub type FormatFn = Arc<dyn Fn(&Record) -> String + Send + Sync>;
12
13pub trait FormatterTrait: Send + Sync + Debug {
15 fn fmt(&self, record: &Record) -> String;
17
18 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 fn with_colors(&mut self, use_colors: bool);
29
30 fn with_timestamp(&mut self, include_timestamp: bool);
32
33 fn with_level(&mut self, include_level: bool);
35
36 fn with_module(&mut self, include_module: bool);
38
39 fn with_location(&mut self, include_location: bool);
41
42 fn with_pattern(&mut self, pattern: String);
44
45 fn with_format(&mut self, format_fn: FormatFn);
47
48 fn box_clone(&self) -> Box<dyn FormatterTrait + Send + Sync>;
50}
51
52#[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 pub fn text() -> Self {
75 Self {
76 inner: Box::new(crate::formatters::text::TextFormatter::default()),
77 }
78 }
79
80 pub fn json() -> Self {
82 Self {
83 inner: Box::new(crate::formatters::json::JsonFormatter::default()),
84 }
85 }
86
87 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 pub fn format(&self, record: &Record) -> String {
98 FormatterTrait::fmt(&*self.inner, record)
99 }
100
101 pub fn format_batch(&self, records: &[Record]) -> String {
103 self.inner.fmt_batch(records)
104 }
105
106 pub fn with_colors(mut self, use_colors: bool) -> Self {
108 self.inner.with_colors(use_colors);
109 self
110 }
111
112 pub fn with_timestamp(mut self, include_timestamp: bool) -> Self {
114 self.inner.with_timestamp(include_timestamp);
115 self
116 }
117
118 pub fn with_level(mut self, include_level: bool) -> Self {
120 self.inner.with_level(include_level);
121 self
122 }
123
124 pub fn with_module(mut self, include_module: bool) -> Self {
126 self.inner.with_module(include_module);
127 self
128 }
129
130 pub fn with_location(mut self, include_location: bool) -> Self {
132 self.inner.with_location(include_location);
133 self
134 }
135
136 pub fn with_pattern(mut self, pattern: impl Into<String>) -> Self {
138 self.inner.with_pattern(pattern.into());
139 self
140 }
141
142 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
152pub use self::json::JsonFormatter;
154pub use self::template::TemplateFormatter;
155pub use self::text::TextFormatter;