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
use std::fmt::Debug;
use slog::{Logger, Drain, FnValue};
use slog_async::Async;
use slog_term::{TermDecorator, CompactFormat, FullFormat};
use {Result, Build, Config};
use misc::{module_and_line, timezone_to_timestamp_fn};
use types::{Format, Severity, TimeZone};
#[derive(Debug)]
pub struct TerminalLoggerBuilder {
format: Format,
timezone: TimeZone,
destination: Destination,
level: Severity,
}
impl TerminalLoggerBuilder {
pub fn new() -> Self {
TerminalLoggerBuilder {
format: Format::default(),
timezone: TimeZone::default(),
destination: Destination::default(),
level: Severity::default(),
}
}
pub fn format(&mut self, format: Format) -> &mut Self {
self.format = format;
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
}
fn build_with_drain<D>(&self, drain: D) -> Logger
where
D: Drain + Send + 'static,
D::Err: Debug,
{
let drain = Async::default(drain.fuse()).fuse();
let drain = self.level.set_level_filter(drain).fuse();
Logger::root(drain, o!("module" => FnValue(module_and_line)))
}
}
impl Build for TerminalLoggerBuilder {
fn build(&self) -> Result<Logger> {
let decorator = self.destination.to_term_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_term_decorator(&self) -> TermDecorator {
match *self {
Destination::Stdout => TermDecorator::new().stdout().build(),
Destination::Stderr => TermDecorator::new().stderr().build(),
}
}
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct TerminalLoggerConfig {
#[serde(default)]
pub level: Severity,
#[serde(default)]
pub format: Format,
#[serde(default)]
pub timezone: TimeZone,
#[serde(default)]
pub destination: Destination,
}
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.timezone(self.timezone);
builder.destination(self.destination);
Ok(builder)
}
}