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
mod json;
#[macro_use]
pub mod macros;
mod message;
mod terminal;
use slog::{o, Drain, Logger as SLogger};
use slog_scope::GlobalLoggerGuard;
use std::sync::Arc;
#[must_use]
pub struct GuardLogger {
pub guard: GlobalLoggerGuard,
}
impl std::fmt::Debug for GuardLogger {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GuardLogger").finish()
}
}
pub mod slog_macro {
pub use slog::{debug, error, info, trace, warn};
pub use slog_scope::with_logger;
}
pub fn json_guard(app_name: &str) -> GuardLogger {
with_async(json_async_drain(app_name))
}
pub fn term_guard(app_name: &str) -> GuardLogger {
with_async(term_async_drain(app_name))
}
fn with_async(drain: slog::Fuse<slog_async::Async>) -> GuardLogger {
let logger = SLogger::root(Arc::new(drain).fuse(), o!());
let scope_guard = slog_scope::set_global_logger(logger);
slog_stdlog::init().expect("Failed to init slog_stdlog compatibility layer");
GuardLogger { guard: scope_guard }
}
fn term_async_drain(app_name: &str) -> slog::Fuse<slog_async::Async> {
let decorator = slog_term::TermDecorator::new().build();
let custom_format = terminal::CustomFormat::new(app_name, decorator);
let env = slog_envlogger::new(custom_format).fuse();
slog_async::Async::new(env).build().fuse()
}
fn json_async_drain(app_name: &str) -> slog::Fuse<slog_async::Async> {
let json = json::JsonDrain::new(app_name, std::io::stdout());
let env = slog_envlogger::new(json).fuse();
slog_async::Async::new(env).build().fuse()
}