Skip to content

Commit 8223adf

Browse files
authored
Allow toggling parts of the default format (#66)
* allow toggling parts of the default format * add a from_default_env method and docs
1 parent 02fda0f commit 8223adf

4 files changed

Lines changed: 312 additions & 68 deletions

File tree

examples/custom_default_format.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*!
2+
Disabling parts of the default 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+
#[macro_use]
21+
extern crate log;
22+
extern crate env_logger;
23+
24+
use env_logger::{Env, Builder};
25+
26+
fn init_logger() {
27+
let env = Env::default()
28+
.filter("MY_LOG_LEVEL")
29+
.write_style("MY_LOG_STYLE");
30+
31+
let mut builder = Builder::from_env(env);
32+
33+
builder
34+
.default_format_level(false)
35+
.default_format_timestamp(false);
36+
37+
builder.init();
38+
}
39+
40+
fn main() {
41+
init_logger();
42+
43+
info!("a log from `MyLogger`");
44+
}

examples/custom_logger.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ Before running this example, try setting the `MY_LOG_LEVEL` environment variable
77
$ export MY_LOG_LEVEL='info'
88
```
99
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-
1710
If you only want to change the way logs are formatted, look at the `custom_format` example.
1811
*/
1912

examples/default.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ $ export MY_LOG_STYLE=never
1919
extern crate log;
2020
extern crate env_logger;
2121

22+
use env_logger::Env;
23+
2224
fn main() {
23-
env_logger::init_from_env("MY_LOG_LEVEL");
25+
let env = Env::default()
26+
.filter("MY_LOG_LEVEL")
27+
.write_style("MY_LOG_STYLE");
28+
29+
env_logger::init_from_env(env);
2430

2531
trace!("some trace log");
2632
debug!("some debug log");

0 commit comments

Comments
 (0)