3 [![Maintenance](https://img.shields.io/badge/maintenance-actively%20maintained-brightgreen.svg)](https://github.com/env-logger-rs/env_logger)
4 [![crates.io](https://img.shields.io/crates/v/env_logger.svg)](https://crates.io/crates/env_logger)
5 [![Documentation](https://docs.rs/env_logger/badge.svg)](https://docs.rs/env_logger)
6 [![Documentation](https://img.shields.io/badge/docs-master-blue.svg)](https://env-logger-rs.github.io/env_logger/env_logger/index.html)
9 Implements a logger that can be configured via environment variables.
15 `env_logger` makes sense when used in executables (binary projects). Libraries should use the [`log`](https://doc.rust-lang.org/log) crate instead.
19 It must be added along with `log` to the project dependencies:
27 `env_logger` must be initialized as early as possible in the project. After it's initialized, you can use the `log` macros to do actual logging.
42 Then when running the executable, specify a value for the **`RUST_LOG`**
43 environment variable that corresponds with the log messages you want to show.
46 $ RUST_LOG=info ./main
47 [2018-11-03T06:09:06Z INFO default] starting up
50 The letter case is not significant for the logging level names; e.g., `debug`,
51 `DEBUG`, and `dEbuG` all represent the same logging level. Therefore, the
52 previous example could also have been written this way, specifying the log
53 level as `INFO` rather than as `info`:
56 $ RUST_LOG=INFO ./main
57 [2018-11-03T06:09:06Z INFO default] starting up
60 So which form should you use? For consistency, our convention is to use lower
61 case names. Where our docs do use other forms, they do so in the context of
62 specific examples, so you won't be surprised if you see similar usage in the
65 The log levels that may be specified correspond to the [`log::Level`][level-enum]
66 enum from the `log` crate. They are:
74 [level-enum]: https://docs.rs/log/latest/log/enum.Level.html "log::Level (docs.rs)"
76 There is also a pseudo logging level, `off`, which may be specified to disable
77 all logging for a given module or for the entire application. As with the
78 logging levels, the letter case is not significant.
80 `env_logger` can be configured in other ways besides an environment variable. See [the examples](https://github.com/env-logger-rs/env_logger/tree/master/examples) for more approaches.
84 Tests can use the `env_logger` crate to see log messages generated during that test:
98 fn add_one(num: i32) -> i32 {
99 info!("add_one called with {}", num);
108 let _ = env_logger::builder().is_test(true).try_init();
115 info!("can log from the test too");
116 assert_eq!(3, add_one(2));
120 fn it_handles_negative_numbers() {
123 info!("logging from another test");
124 assert_eq!(-7, add_one(-8));
129 Assuming the module under test is called `my_lib`, running the tests with the
130 `RUST_LOG` filtering to info messages from this module looks like:
133 $ RUST_LOG=my_lib=info cargo test
134 Running target/debug/my_lib-...
137 [INFO my_lib::tests] logging from another test
138 [INFO my_lib] add_one called with -8
139 test tests::it_handles_negative_numbers ... ok
140 [INFO my_lib::tests] can log from the test too
141 [INFO my_lib] add_one called with 2
142 test tests::it_adds_one ... ok
144 test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured
147 Note that `env_logger::try_init()` needs to be called in each test in which you
148 want to enable logging. Additionally, the default behavior of tests to
149 run in parallel means that logging output may be interleaved with test output.
150 Either run tests in a single thread by specifying `RUST_TEST_THREADS=1` or by
151 running one test by specifying its name as an argument to the test binaries as
152 directed by the `cargo test` help docs:
155 $ RUST_LOG=my_lib=info cargo test it_adds_one
156 Running target/debug/my_lib-...
159 [INFO my_lib::tests] can log from the test too
160 [INFO my_lib] add_one called with 2
161 test tests::it_adds_one ... ok
163 test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
166 ## Configuring log target
168 By default, `env_logger` logs to stderr. If you want to log to stdout instead,
169 you can use the `Builder` to change the log target:
173 use env_logger::{Builder, Target};
175 let mut builder = Builder::from_default_env();
176 builder.target(Target::Stdout);
181 ## Stability of the default format
183 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`.
185 If you want to capture or interpret the output of `env_logger` programmatically then you should use a custom format.