1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use log::LevelFilter;
use serde::Deserialize;
use std::borrow::Cow;
const DEFAULT_TARGET_WIDTH: usize = 42;
const DEFAULT_LEVEL_WIDTH: usize = 5;
const DEFAULT_OUTPUT_NAME: &str = crate::LOGGER_STDOUT_NAME;
const DEFAULT_OUTPUT_LEVEL_FILTER: LevelFilter = LevelFilter::Info;
const DEFAULT_COLOR_ENABLED: bool = false;
#[derive(Default, Deserialize)]
#[must_use]
pub struct LoggerOutputConfigBuilder {
name: Option<String>,
level_filter: Option<LevelFilter>,
target_filters: Option<Vec<String>>,
target_exclusions: Option<Vec<String>>,
color_enabled: Option<bool>,
}
impl LoggerOutputConfigBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn name<'a>(mut self, name: impl Into<Cow<'a, str>>) -> Self {
self.name.replace(name.into().into_owned());
self
}
pub fn level_filter(mut self, level: LevelFilter) -> Self {
self.level_filter.replace(level);
self
}
pub fn target_filters(mut self, target_filters: &[&str]) -> Self {
self.target_filters = Some(
target_filters
.iter()
.map(|f| f.to_string())
.collect::<Vec<String>>(),
);
self
}
pub fn color_enabled(mut self, color: bool) -> Self {
self.color_enabled.replace(color);
self
}
#[must_use]
pub fn finish(self) -> LoggerOutputConfig {
LoggerOutputConfig {
name: self.name.unwrap_or_else(|| DEFAULT_OUTPUT_NAME.to_owned()),
level_filter: self.level_filter.unwrap_or(DEFAULT_OUTPUT_LEVEL_FILTER),
target_filters: self
.target_filters
.unwrap_or_default()
.iter()
.map(|f| f.to_lowercase())
.collect(),
target_exclusions: self
.target_exclusions
.unwrap_or_default()
.iter()
.map(|f| f.to_lowercase())
.collect(),
color_enabled: self.color_enabled.unwrap_or(DEFAULT_COLOR_ENABLED),
}
}
}
#[derive(Clone)]
pub struct LoggerOutputConfig {
pub(crate) name: String,
pub(crate) level_filter: LevelFilter,
pub(crate) target_filters: Vec<String>,
pub(crate) target_exclusions: Vec<String>,
pub(crate) color_enabled: bool,
}
#[derive(Default, Deserialize)]
#[must_use]
pub struct LoggerConfigBuilder {
target_width: Option<usize>,
level_width: Option<usize>,
outputs: Option<Vec<LoggerOutputConfigBuilder>>,
}
impl LoggerConfigBuilder {
pub fn with_target_width(mut self, width: usize) -> Self {
self.target_width.replace(width);
self
}
pub fn with_level_width(mut self, width: usize) -> Self {
self.level_width.replace(width);
self
}
pub fn with_output(mut self, output: LoggerOutputConfigBuilder) -> Self {
self.outputs.get_or_insert_with(Vec::new).push(output);
self
}
pub fn level<'a>(&mut self, name: impl Into<Cow<'a, str>>, level: LevelFilter) {
let name = name.into();
if let Some(outputs) = self.outputs.as_deref_mut() {
if let Some(stdout) = outputs
.iter_mut()
.find(|output| match output.name.as_ref() {
Some(output_name) => output_name[..] == name,
None => false,
})
{
stdout.level_filter.replace(level);
}
}
}
#[must_use]
pub fn finish(self) -> LoggerConfig {
let outputs = self
.outputs
.map(|os| os.into_iter().map(|o| o.finish()).collect())
.unwrap_or_default();
LoggerConfig {
target_width: self.target_width.unwrap_or(DEFAULT_TARGET_WIDTH),
level_width: self.level_width.unwrap_or(DEFAULT_LEVEL_WIDTH),
outputs,
}
}
}
#[derive(Clone)]
pub struct LoggerConfig {
pub(crate) target_width: usize,
pub(crate) level_width: usize,
pub(crate) outputs: Vec<LoggerOutputConfig>,
}
impl LoggerConfig {
pub fn build() -> LoggerConfigBuilder {
LoggerConfigBuilder::default()
}
pub fn target_width(&self) -> usize {
self.target_width
}
pub fn level_width(&self) -> usize {
self.level_width
}
pub fn outputs(&self) -> &[LoggerOutputConfig] {
&self.outputs
}
}