-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.rs
110 lines (90 loc) · 3.3 KB
/
logger.rs
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
extern crate log;
extern crate log4rs;
use crate::lib::get_executable_folder;
use crate::{LOG_CONFIG, LOG_PATTERN};
use std;
use std::default::Default;
use std::fs::metadata;
use std::path::Path;
use log::LevelFilter;
use log4rs::append::console::ConsoleAppender;
use log4rs::append::file::FileAppender;
use log4rs::config::{Appender, Config, Logger, Root};
use log4rs::encode::pattern::PatternEncoder;
const APPENDER_FILE: &'static str = "file";
const APPENDER_STDOUT: &'static str = "stdout";
/// Initializes the logger.
pub fn initialize() {
let mut log_path = get_executable_folder().unwrap_or(std::path::PathBuf::from("/"));
log_path.push(LOG_CONFIG);
println!("Logging file: {:?}", log_path);
match metadata(log_path.as_path()) {
Ok(v) => {
if v.is_file() {
init_file(&log_path);
return;
}
}
Err(e) => println!("Error for log config: {:?}", e),
}
init_config(); // call fallback
}
/// Initialize log config from file
fn init_file(conf: &Path) {
match log4rs::init_file(conf, Default::default()) {
Ok(_) => (),
Err(e) => panic!("Log initialization failed! {:?}", e),
}
}
/// Initialize a fallback configurated logger.
/// Consisting of log to conole & if possible to file.
fn init_config() {
let stdout_appender = ConsoleAppender::builder()
.encoder(Box::new(PatternEncoder::new(LOG_PATTERN)))
.build();
let file_appender = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new(LOG_PATTERN)))
.build("log/default.log");
let file_success = file_appender.is_ok();
let mut root_builder = Root::builder().appender(APPENDER_STDOUT);
if file_success {
root_builder = root_builder.appender(APPENDER_FILE);
}
let root = root_builder.build(LevelFilter::Warn);
let mut config_builder = Config::builder()
.appender(Appender::builder().build(APPENDER_STDOUT, Box::new(stdout_appender)));
if file_success {
config_builder = config_builder
.appender(Appender::builder().build(APPENDER_FILE, Box::new(file_appender.unwrap())));
}
config_builder =
config_builder.logger(Logger::builder().build("yayd_backend", LevelFilter::max()));
let config = config_builder.build(root).unwrap();
println!("Log fallback init: {}", log4rs::init_config(config).is_ok());
trace!("Log fallback init");
if !file_success {
// print after log init, useless otherwise
error!("Could not initialize file based logging!");
}
warn!(
"No log config file found, please create file {}",
LOG_CONFIG
);
warn!("According to https://github.com/sfackler/log4rs");
info!("Using internal logging configuration on most verbose level.");
}
/// Test logger configuration, without file support, ignoring external configs
#[allow(unused)]
pub fn init_config_test() {
let console = ConsoleAppender::builder()
.encoder(Box::new(PatternEncoder::new(LOG_PATTERN)))
.build();
let root = Root::builder()
.appender(APPENDER_STDOUT)
.build(LevelFilter::max());
let _config = Config::builder()
.appender(Appender::builder().build(APPENDER_STDOUT, Box::new(console)))
.build(root)
.unwrap();
info!("Test logger configuration");
}