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
use slog::Drain;
pub struct PrintlnDrain {
pub min_level: slog::Level,
}
impl slog::Drain for PrintlnDrain {
type Ok = ();
type Err = std::io::Error;
fn log(
&self,
record: &slog::Record,
_values: &slog::OwnedKVList,
) -> Result<Self::Ok, Self::Err> {
if record.level().is_at_least(self.min_level) {
println!("{}", record.msg());
}
Ok(())
}
}
pub struct LoggerContext {
pub logger: slog::Logger,
}
pub fn logger_from_env(min_level: slog::Level) -> LoggerContext {
LoggerContext {
logger: slog::Logger::root(PrintlnDrain { min_level }.fuse(), slog::o!()),
}
}
impl Default for LoggerContext {
fn default() -> Self {
LoggerContext {
logger: slog::Logger::root(
PrintlnDrain {
min_level: slog::Level::Warning,
}
.fuse(),
slog::o!(),
),
}
}
}