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
use slog::{b, Level};
struct SlogBackend;
fn log_to_slog_level(level: log::Level) -> Level {
match level {
log::Level::Trace => Level::Trace,
log::Level::Debug => Level::Debug,
log::Level::Info => Level::Info,
log::Level::Warn => Level::Warning,
log::Level::Error => Level::Error,
}
}
fn slog_to_log_level(level: Level) -> log::Level {
match level {
Level::Critical | Level::Error => log::Level::Error,
Level::Warning => log::Level::Warn,
Level::Debug => log::Level::Debug,
Level::Trace => log::Level::Trace,
Level::Info => log::Level::Info,
}
}
impl log::Log for SlogBackend {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
}
fn log(&self, r: &log::Record) {
let level = log_to_slog_level(r.metadata().level());
let args = r.args();
let target = r.target();
let module = r.module_path_static().unwrap_or("");
let file = r.file_static().unwrap_or("");
let line = r.line().unwrap_or(0);
let s = slog::RecordStatic {
location: &slog::RecordLocation {
file,
line,
column: 0,
function: "",
module,
},
level,
tag: target,
};
crate::borrow_global().log(&slog::Record::new(&s, args, b!()))
}
fn flush(&self) {}
}
pub fn redirect_std_log(level: Option<Level>) -> Result<(), log::SetLoggerError> {
if let Some(level) = level {
log::set_max_level(slog_to_log_level(level).to_level_filter());
}
log::set_logger(&SlogBackend)
}