Skip to content

Commit 89bcabf

Browse files
authored
Add some more examples for configuring env_logger (#133)
1 parent 2d183bb commit 89bcabf

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ $ RUST_LOG=info ./main
4343
[2018-11-03T06:09:06Z INFO default] starting up
4444
```
4545

46+
`env_logger` can be configured in other ways besides an environment variable. See [the examples](https://github.com/sebasmagri/env_logger/tree/master/examples) for more approaches.
47+
4648
### In tests
4749

4850
Tests can use the `env_logger` crate to see log messages generated during that test:
@@ -147,4 +149,4 @@ builder.init();
147149

148150
The default format won't optimise for long-term stability, and explicitly makes no guarantees about the stability of its output across major, minor or patch version bumps during `0.x`.
149151

150-
If you want to capture or interpret the output of `env_logger` programmatically then you should use a custom format.
152+
If you want to capture or interpret the output of `env_logger` programmatically then you should use a custom format.

examples/default.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ extern crate env_logger;
2222
use env_logger::Env;
2323

2424
fn main() {
25+
// The `Env` lets us tweak what the environment
26+
// variables to read are and what the default
27+
// value is if they're missing
2528
let env = Env::default()
2629
.filter_or("MY_LOG_LEVEL", "trace")
2730
.write_style_or("MY_LOG_STYLE", "always");

examples/filters_from_code.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*!
2+
Specify logging filters in code instead of using an environment variable.
3+
*/
4+
5+
#[macro_use]
6+
extern crate log;
7+
extern crate env_logger;
8+
9+
use env_logger::Env;
10+
11+
fn main() {
12+
env_logger::builder()
13+
.filter_level(log::LevelFilter::Trace)
14+
.init();
15+
16+
trace!("some trace log");
17+
debug!("some debug log");
18+
info!("some information log");
19+
warn!("some warning log");
20+
error!("some error log");
21+
}

0 commit comments

Comments
 (0)