1 /*!
2 Changing the default logging format.
3
4 Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`:
5
6 ```no_run,shell
7 $ export MY_LOG_LEVEL='info'
8 ```
9
10 Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors
11 or `auto` to enable them:
12
13 ```no_run,shell
14 $ export MY_LOG_STYLE=never
15 ```
16
17 If you want to control the logging output completely, see the `custom_logger` example.
18 */
19
20 #[cfg(all(feature = "termcolor", feature = "humantime"))]
main()21 fn main() {
22 use env_logger::{fmt::Color, Builder, Env};
23
24 use std::io::Write;
25
26 fn init_logger() {
27 let env = Env::default()
28 .filter("MY_LOG_LEVEL")
29 .write_style("MY_LOG_STYLE");
30
31 Builder::from_env(env)
32 .format(|buf, record| {
33 let mut style = buf.style();
34 style.set_bg(Color::Yellow).set_bold(true);
35
36 let timestamp = buf.timestamp();
37
38 writeln!(
39 buf,
40 "My formatted log ({}): {}",
41 timestamp,
42 style.value(record.args())
43 )
44 })
45 .init();
46 }
47
48 init_logger();
49
50 log::info!("a log from `MyLogger`");
51 }
52
53 #[cfg(not(all(feature = "termcolor", feature = "humantime")))]
main()54 fn main() {}
55