rust_loguru/formatters/
mod.rs1use crate::record::Record;
2use std::fmt::Debug;
3
4#[derive(Debug, Clone)]
6pub enum Formatter {
7 Text(TextFormatter),
8 Json(JsonFormatter),
9 Template(TemplateFormatter),
10}
11
12impl Formatter {
13 pub fn text() -> Self {
14 Self::Text(TextFormatter::new())
15 }
16
17 pub fn json() -> Self {
18 Self::Json(JsonFormatter::new())
19 }
20
21 pub fn template() -> Self {
22 Self::Template(TemplateFormatter::new())
23 }
24
25 pub fn format(&self, record: &Record) -> String {
26 match self {
27 Self::Text(f) => f.format(record),
28 Self::Json(f) => f.format(record),
29 Self::Template(f) => f.format(record),
30 }
31 }
32
33 pub fn with_colors(self, use_colors: bool) -> Self {
34 match self {
35 Self::Text(f) => Self::Text(f.with_colors(use_colors)),
36 Self::Json(f) => Self::Json(f.with_colors(use_colors)),
37 Self::Template(f) => Self::Template(f.with_colors(use_colors)),
38 }
39 }
40
41 pub fn with_timestamp(self, include_timestamp: bool) -> Self {
42 match self {
43 Self::Text(f) => Self::Text(f.with_timestamp(include_timestamp)),
44 Self::Json(f) => Self::Json(f.with_timestamp(include_timestamp)),
45 Self::Template(f) => Self::Template(f.with_timestamp(include_timestamp)),
46 }
47 }
48
49 pub fn with_level(self, include_level: bool) -> Self {
50 match self {
51 Self::Text(f) => Self::Text(f.with_level(include_level)),
52 Self::Json(f) => Self::Json(f.with_level(include_level)),
53 Self::Template(f) => Self::Template(f.with_level(include_level)),
54 }
55 }
56
57 pub fn with_module(self, include_module: bool) -> Self {
58 match self {
59 Self::Text(f) => Self::Text(f.with_module(include_module)),
60 Self::Json(f) => Self::Json(f.with_module(include_module)),
61 Self::Template(f) => Self::Template(f.with_module(include_module)),
62 }
63 }
64
65 pub fn with_location(self, include_location: bool) -> Self {
66 match self {
67 Self::Text(f) => Self::Text(f.with_location(include_location)),
68 Self::Json(f) => Self::Json(f.with_location(include_location)),
69 Self::Template(f) => Self::Template(f.with_location(include_location)),
70 }
71 }
72
73 pub fn with_pattern(self, pattern: impl Into<String>) -> Self {
74 match self {
75 Self::Text(f) => Self::Text(f.with_pattern(pattern)),
76 Self::Json(f) => Self::Json(f.with_pattern(pattern)),
77 Self::Template(f) => Self::Template(f.with_pattern(pattern)),
78 }
79 }
80
81 pub fn with_format<F>(self, format_fn: F) -> Self
82 where
83 F: Fn(&Record) -> String + Send + Sync + 'static,
84 {
85 match self {
86 Self::Text(f) => Self::Text(f.with_format(format_fn)),
87 Self::Json(f) => Self::Json(f.with_format(format_fn)),
88 Self::Template(f) => Self::Template(f.with_format(format_fn)),
89 }
90 }
91}
92
93pub trait FormatterTrait: Send + Sync + Debug {
95 fn format(&self, record: &Record) -> String;
97
98 fn with_colors(self, use_colors: bool) -> Self
100 where
101 Self: Sized;
102
103 fn with_timestamp(self, include_timestamp: bool) -> Self
105 where
106 Self: Sized;
107
108 fn with_level(self, include_level: bool) -> Self
110 where
111 Self: Sized;
112
113 fn with_module(self, include_module: bool) -> Self
115 where
116 Self: Sized;
117
118 fn with_location(self, include_location: bool) -> Self
120 where
121 Self: Sized;
122
123 fn with_pattern(self, pattern: impl Into<String>) -> Self
125 where
126 Self: Sized;
127
128 fn with_format<F>(self, format_fn: F) -> Self
130 where
131 F: Fn(&Record) -> String + Send + Sync + 'static,
132 Self: Sized;
133
134 fn box_clone(&self) -> Box<dyn FormatterTrait + Send + Sync>;
136}
137
138pub mod json;
139pub mod template;
140pub mod text;
141pub mod util;
142
143pub use self::json::JsonFormatter;
144pub use self::template::TemplateFormatter;
145pub use self::text::TextFormatter;