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
60 lines (52 loc) · 1.55 KB
/
logging.rs
File metadata and controls
60 lines (52 loc) · 1.55 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
49
50
51
52
53
54
55
56
57
58
59
60
//! Setup for the application logging.
//!
//! It redirects the log info to the standard output with the log level defined in the configuration.
//!
//! - `Off`
//! - `Error`
//! - `Warn`
//! - `Info`
//! - `Debug`
//! - `Trace`
//!
//! Refer to the [configuration crate documentation](https://docs.rs/torrust-tracker-configuration) to know how to change log settings.
use std::str::FromStr;
use std::sync::Once;
use log::{info, LevelFilter};
use torrust_tracker_configuration::Configuration;
static INIT: Once = Once::new();
/// It redirects the log info to the standard output with the log level defined in the configuration
pub fn setup(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.");
}