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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use slog::{self, Drain, FnValue, Logger};
use slog_async::Async;
use slog_kvfilter::KVFilter;
use slog_term::{self, CompactFormat, FullFormat, PlainDecorator, TermDecorator};
use std::fmt::Debug;
use std::io;
use misc::{module_and_line, timezone_to_timestamp_fn};
use types::KVFilterParameters;
use types::{Format, OverflowStrategy, Severity, SourceLocation, TimeZone};
use {Build, Config, Result};
#[derive(Debug)]
pub struct TerminalLoggerBuilder {
format: Format,
source_location: SourceLocation,
timezone: TimeZone,
destination: Destination,
overflow_strategy: OverflowStrategy,
level: Severity,
channel_size: usize,
kvfilterparameters: Option<KVFilterParameters>,
}
impl TerminalLoggerBuilder {
pub fn new() -> Self {
TerminalLoggerBuilder {
format: Format::default(),
source_location: SourceLocation::default(),
overflow_strategy: OverflowStrategy::default(),
timezone: TimeZone::default(),
destination: Destination::default(),
level: Severity::default(),
channel_size: 1024,
kvfilterparameters: None,
}
}
pub fn format(&mut self, format: Format) -> &mut Self {
self.format = format;
self
}
pub fn source_location(&mut self, source_location: SourceLocation) -> &mut Self {
self.source_location = source_location;
self
}
pub fn overflow_strategy(&mut self, overflow_strategy: OverflowStrategy) -> &mut Self {
self.overflow_strategy = overflow_strategy;
self
}
pub fn timezone(&mut self, timezone: TimeZone) -> &mut Self {
self.timezone = timezone;
self
}
pub fn destination(&mut self, destination: Destination) -> &mut Self {
self.destination = destination;
self
}
pub fn level(&mut self, severity: Severity) -> &mut Self {
self.level = severity;
self
}
pub fn channel_size(&mut self, channel_size: usize) -> &mut Self {
self.channel_size = channel_size;
self
}
pub fn kvfilter(&mut self, parameters: KVFilterParameters) -> &mut Self {
self.kvfilterparameters = Some(parameters);
self
}
fn build_with_drain<D>(&self, drain: D) -> Logger
where
D: Drain + Send + 'static,
D::Err: Debug,
{
let drain = Async::new(drain.fuse())
.chan_size(self.channel_size)
.overflow_strategy(self.overflow_strategy.to_async_type())
.build()
.fuse();
if let Some(ref p) = self.kvfilterparameters {
let kvdrain = KVFilter::new(drain, p.severity.as_level())
.always_suppress_any(p.always_suppress_any.clone())
.only_pass_any_on_all_keys(p.only_pass_any_on_all_keys.clone())
.always_suppress_on_regex(p.always_suppress_on_regex.clone())
.only_pass_on_regex(p.only_pass_on_regex.clone());
let drain = self.level.set_level_filter(kvdrain.fuse());
match self.source_location {
SourceLocation::None => Logger::root(drain.fuse(), o!()),
SourceLocation::ModuleAndLine => {
Logger::root(drain.fuse(), o!("module" => FnValue(module_and_line)))
}
}
} else {
let drain = self.level.set_level_filter(drain.fuse());
match self.source_location {
SourceLocation::None => Logger::root(drain.fuse(), o!()),
SourceLocation::ModuleAndLine => {
Logger::root(drain.fuse(), o!("module" => FnValue(module_and_line)))
}
}
}
}
}
impl Default for TerminalLoggerBuilder {
fn default() -> Self {
Self::new()
}
}
impl Build for TerminalLoggerBuilder {
fn build(&self) -> Result<Logger> {
let decorator = self.destination.to_decorator();
let timestamp = timezone_to_timestamp_fn(self.timezone);
let logger = match self.format {
Format::Full => {
let format = FullFormat::new(decorator).use_custom_timestamp(timestamp);
self.build_with_drain(format.build())
}
Format::Compact => {
let format = CompactFormat::new(decorator).use_custom_timestamp(timestamp);
self.build_with_drain(format.build())
}
};
Ok(logger)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Destination {
Stdout,
Stderr,
}
impl Default for Destination {
fn default() -> Self {
Destination::Stdout
}
}
impl Destination {
fn to_decorator(self) -> Decorator {
let maybe_term_decorator = match self {
Destination::Stdout => TermDecorator::new().stdout().try_build(),
Destination::Stderr => TermDecorator::new().stderr().try_build(),
};
maybe_term_decorator
.map(Decorator::Term)
.unwrap_or_else(|| match self {
Destination::Stdout => Decorator::PlainStdout(PlainDecorator::new(io::stdout())),
Destination::Stderr => Decorator::PlainStderr(PlainDecorator::new(io::stderr())),
})
}
}
enum Decorator {
Term(TermDecorator),
PlainStdout(PlainDecorator<io::Stdout>),
PlainStderr(PlainDecorator<io::Stderr>),
}
impl slog_term::Decorator for Decorator {
fn with_record<F>(
&self,
record: &slog::Record,
logger_values: &slog::OwnedKVList,
f: F,
) -> io::Result<()>
where
F: FnOnce(&mut slog_term::RecordDecorator) -> io::Result<()>,
{
match *self {
Decorator::Term(ref d) => d.with_record(record, logger_values, f),
Decorator::PlainStdout(ref d) => d.with_record(record, logger_values, f),
Decorator::PlainStderr(ref d) => d.with_record(record, logger_values, f),
}
}
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct TerminalLoggerConfig {
#[serde(default)]
pub level: Severity,
#[serde(default)]
pub format: Format,
#[serde(default)]
pub source_location: SourceLocation,
#[serde(default)]
pub timezone: TimeZone,
#[serde(default)]
pub destination: Destination,
#[serde(default = "default_channel_size")]
pub channel_size: usize,
#[serde(default)]
pub overflow_strategy: OverflowStrategy,
}
impl Config for TerminalLoggerConfig {
type Builder = TerminalLoggerBuilder;
fn try_to_builder(&self) -> Result<Self::Builder> {
let mut builder = TerminalLoggerBuilder::new();
builder.level(self.level);
builder.format(self.format);
builder.source_location(self.source_location);
builder.timezone(self.timezone);
builder.destination(self.destination);
builder.channel_size(self.channel_size);
builder.overflow_strategy(self.overflow_strategy);
Ok(builder)
}
}
fn default_channel_size() -> usize {
1024
}