-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure_logging.rs
54 lines (46 loc) · 1.18 KB
/
configure_logging.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
use log::{debug, info, warn, LevelFilter};
use log4rs::{
append::file::FileAppender,
config::{Appender, Config, Root},
encode::pattern::PatternEncoder,
};
use std::error::Error;
pub mod foo {
use super::*;
pub mod bar {
use super::*;
pub fn run() {
warn!("[bar] warn");
info!("[bar] info");
debug!("[bar] debug");
}
}
pub fn run() {
warn!("[foo] warn");
info!("[foo] info");
debug!("[foo] debug");
bar::run();
}
}
/// Configure logging to write to a file
///
/// # Examples
///
/// ```ignore
/// use log::info;
/// use development_tools::log_to_file;
///
/// let result = log_to_file();
/// info!("Doc test!");
/// assert!(result.is_ok());
/// ```
pub fn log_to_file() -> Result<(), Box<dyn Error>> {
let logfile = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new("{l} - {m}\n")))
.build("log/output.log")?;
let config = Config::builder()
.appender(Appender::builder().build("logfile", Box::new(logfile)))
.build(Root::builder().appender("logfile").build(LevelFilter::Info))?;
log4rs::init_config(config)?;
Ok(())
}