forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.rs
More file actions
48 lines (39 loc) · 1.08 KB
/
logging.rs
File metadata and controls
48 lines (39 loc) · 1.08 KB
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
use std::str::FromStr;
use std::sync::Once;
use log::{info, LevelFilter};
use crate::Configuration;
static INIT: Once = Once::new();
pub fn setup_logging(cfg: &Configuration) {
let level = config_level_or_default(&cfg.log_level);
if level == log::LevelFilter::Off {
return;
}
INIT.call_once(|| {
stdout_config(level);
});
}
fn config_level_or_default(log_level: &Option<String>) -> LevelFilter {
match log_level {
None => log::LevelFilter::Info,
Some(level) => LevelFilter::from_str(level).unwrap(),
}
}
fn stdout_config(level: LevelFilter) {
if let Err(_err) = fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"{} [{}][{}] {}",
chrono::Local::now().format("%+"),
record.target(),
record.level(),
message
))
})
.level(level)
.chain(std::io::stdout())
.apply()
{
panic!("Failed to initialize logging.")
}
info!("logging initialized.");
}