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
use chrono::prelude::*;
use chrono::Local;
use slog::Record;
use slog::{o, Drain, FilterLevel, Fuse, Logger};
use slog::{FnValue, PushFnValue};
use slog_async::Async;
use slog_envlogger::LogBuilder as EnvLogBuilder;
use slog_json::Json;
use slog_term::{CompactFormat, TermDecorator};
use std::env;
pub struct SlogKickstarter {
default_filter_level: FilterLevel,
debug_modules: Vec<&'static str>,
service_name: String,
init_std_log: bool,
use_json_logging: bool,
}
impl SlogKickstarter {
#[must_use]
pub fn new<S: Into<String>>(service_name: S) -> Self {
let use_json_logging = env::var("RUST_LOG_JSON")
.map(|v| v == "1")
.unwrap_or_default();
Self {
default_filter_level: FilterLevel::Info,
debug_modules: vec![],
service_name: service_name.into(),
init_std_log: true,
use_json_logging,
}
}
pub fn with_debug_log_for(&mut self, module_name: &'static str) -> &mut Self {
self.debug_modules.push(module_name);
self
}
pub fn with_default_level(&mut self, level: FilterLevel) -> &mut Self {
self.default_filter_level = level;
self
}
pub fn with_json_logging(&mut self) -> &mut Self {
self.use_json_logging = true;
self
}
pub fn without_json_logging(&mut self) -> &mut Self {
self.use_json_logging = false;
self
}
pub fn without_stdlog(&mut self) -> &mut Self {
self.init_std_log = false;
self
}
#[must_use]
pub fn init(&self) -> Logger {
let drain = if self.use_json_logging {
self.setup_json_logging()
} else {
self.setup_term_logging()
};
if self.init_std_log {
let _guard = slog_stdlog::init();
}
slog::Logger::root(
drain,
o!("service" => self.service_name.to_owned(), "log_type" => "application", "application_type" => "service", "module" => FnValue(move |info| {
info.module().to_string()
})
),
)
}
fn setup_json_logging(&self) -> Fuse<Async> {
let drain = Json::new(std::io::stdout())
.add_key_value(o!(
"@timestamp" => PushFnValue(move |_ : &Record, ser| {
ser.emit(Local::now().to_rfc3339_opts(SecondsFormat::Secs, true))
}),
"loglevel" => FnValue(move |rinfo : &Record| {
rinfo.level().as_str()
}),
"msg" => PushFnValue(move |record : &Record, ser| {
ser.emit(record.msg())
}),
))
.build()
.fuse();
let builder = EnvLogBuilder::new(drain)
.filter(None, self.default_filter_level);
let builder = self.debug_modules.iter().fold(builder, |b, &module_name| {
b.filter(Some(module_name), FilterLevel::Debug)
});
let drain = builder
.parse(env::var("RUST_LOG").unwrap_or_default().as_str())
.build()
.fuse();
slog_async::Async::new(drain).build().fuse()
}
fn setup_term_logging(&self) -> Fuse<Async> {
let decorator = TermDecorator::new().build();
let drain = CompactFormat::new(decorator).build().fuse();
let builder = EnvLogBuilder::new(drain).filter(None, self.default_filter_level);
let builder = self.debug_modules.iter().fold(builder, |b, &module_name| {
b.filter(Some(module_name), FilterLevel::Debug)
});
let drain = builder
.parse(env::var("RUST_LOG").unwrap_or_default().as_str())
.build()
.fuse();
slog_async::Async::new(drain).build().fuse()
}
}