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
#[allow(unused_imports)]
#[macro_use]
extern crate slog;
mod collector_serializer;
mod glog_format;
mod kv_categorizer;
mod security;
mod simple_logger;
use crate::kv_categorizer::ErrorCategorizer;
use glog_format::GlogFormat;
use lazy_static::lazy_static;
use slog::{o, Discard, Drain, FilterLevel, Logger, Never};
pub use slog::{slog_crit, slog_debug, slog_error, slog_info, slog_trace, slog_warn};
use slog_async::Async;
use slog_envlogger::{EnvLogger, LogBuilder};
pub use slog_scope::{crit, debug, error, info, trace, warn};
use slog_scope::{set_global_logger, GlobalLoggerGuard};
use slog_term::{PlainDecorator, TermDecorator};
use std::sync::Mutex;
pub mod prelude {
pub use crate::security::{security_log, SecurityEvent};
pub use slog::{slog_crit, slog_debug, slog_error, slog_info, slog_trace, slog_warn};
pub use slog_scope::{crit, debug, error, info, trace, warn};
}
pub use simple_logger::{set_simple_logger, set_simple_logger_prefix};
pub fn set_default_global_logger(async_drain: bool, chan_size: Option<usize>) -> GlobalLoggerGuard {
let logger = create_default_root_logger(async_drain, chan_size);
set_global_logger(logger)
}
fn create_default_root_logger(async_drain: bool, chan_size: Option<usize>) -> Logger {
let drain = GlogFormat::new(PlainDecorator::new(::std::io::stderr()), ErrorCategorizer).fuse();
let logger = create_env_logger(drain);
get_logger(async_drain, chan_size, logger)
}
fn create_env_logger_with_level<D>(drain: D, level: FilterLevel) -> EnvLogger<D>
where
D: Drain<Err = Never, Ok = ()> + Send + 'static,
{
let mut builder = LogBuilder::new(drain);
builder = builder.filter(None, level);
if let Ok(s) = ::std::env::var("RUST_LOG") {
builder = builder.parse(&s);
}
builder.build()
}
fn create_env_logger<D>(drain: D) -> EnvLogger<D>
where
D: Drain<Err = Never, Ok = ()> + Send + 'static,
{
create_env_logger_with_level(drain, FilterLevel::Info)
}
fn create_test_root_logger() -> Logger {
let drain = GlogFormat::new(TermDecorator::new().build(), ErrorCategorizer).fuse();
let envlogger = create_env_logger_with_level(drain, FilterLevel::Debug);
Logger::root(Mutex::new(envlogger).fuse(), o!())
}
lazy_static! {
static ref TESTING_ENVLOGGER_GUARD: GlobalLoggerGuard = {
let logger = {
if ::std::env::var("RUST_LOG").is_ok() {
create_default_root_logger(false , None )
} else {
Logger::root(Discard, o!())
}
};
set_global_logger(logger)
};
static ref END_TO_END_TESTING_ENVLOGGER_GUARD: GlobalLoggerGuard = {
let logger = create_test_root_logger();
set_global_logger(logger)
};
}
pub fn try_init_for_testing() {
::lazy_static::initialize(&TESTING_ENVLOGGER_GUARD);
}
pub fn init_for_e2e_testing() {
::lazy_static::initialize(&END_TO_END_TESTING_ENVLOGGER_GUARD);
}
fn get_logger<D>(is_async: bool, chan_size: Option<usize>, drain: D) -> Logger
where
D: Drain<Err = Never, Ok = ()> + Send + 'static,
{
if is_async {
let async_builder = match chan_size {
Some(chan_size_inner) => Async::new(drain).chan_size(chan_size_inner),
None => Async::new(drain),
};
Logger::root(async_builder.build().fuse(), o!())
} else {
Logger::root(Mutex::new(drain).fuse(), o!())
}
}