Crate env_logger[][src]

A simple logger that can be configured via environment variables, for use with the logging facade exposed by the log crate.

Despite having “env” in its name, env_logger can also be configured by other means besides environment variables. See the examples in the source repository for more approaches.

By default, env_logger writes logs to stderr, but can be configured to instead write them to stdout.


use log::{debug, error, log_enabled, info, Level};


debug!("this is a debug {}", "message");
error!("this is printed by default");

if log_enabled!(Level::Info) {
    let x = 3 * 4; // expensive computation
    info!("the answer was: {}", x);

Assumes the binary is main:

$ RUST_LOG=error ./main
[2017-11-09T02:12:24Z ERROR main] this is printed by default
$ RUST_LOG=info ./main
[2017-11-09T02:12:24Z ERROR main] this is printed by default
[2017-11-09T02:12:24Z INFO main] the answer was: 12
$ RUST_LOG=debug ./main
[2017-11-09T02:12:24Z DEBUG main] this is a debug message
[2017-11-09T02:12:24Z ERROR main] this is printed by default
[2017-11-09T02:12:24Z INFO main] the answer was: 12

You can also set the log level on a per module basis:

$ RUST_LOG=main=info ./main
[2017-11-09T02:12:24Z ERROR main] this is printed by default
[2017-11-09T02:12:24Z INFO main] the answer was: 12

And enable all logging:

$ RUST_LOG=main ./main
[2017-11-09T02:12:24Z DEBUG main] this is a debug message
[2017-11-09T02:12:24Z ERROR main] this is printed by default
[2017-11-09T02:12:24Z INFO main] the answer was: 12

If the binary name contains hyphens, you will need to replace them with underscores:

$ RUST_LOG=my_app ./my-app
[2017-11-09T02:12:24Z DEBUG my_app] this is a debug message
[2017-11-09T02:12:24Z ERROR my_app] this is printed by default
[2017-11-09T02:12:24Z INFO my_app] the answer was: 12

This is because Rust modules and crates cannot contain hyphens in their name, although cargo continues to accept them.

See the documentation for the log crate for more information about its API.

Enabling logging

Log levels are controlled on a per-module basis, and by default all logging is disabled except for the error level.

Logging is controlled via the RUST_LOG environment variable. The value of this environment variable is a comma-separated list of logging directives. A logging directive is of the form:


The path to the module is rooted in the name of the crate it was compiled for, so if your program is contained in a file, for example, to turn on logging for this file you would use a value of RUST_LOG=hello. Furthermore, this path is a prefix-search, so all modules nested in the specified module will also have logging enabled.

When providing the crate name or a module path, explicitly specifying the log level is optional. If omitted, all logging for the item (and its children) will be enabled.

The names of the log levels that may be specified correspond to the variations of the log::Level enum from the log crate. They are:

There is also a pseudo logging level, off, which may be specified to disable all logging for a given module or for the entire application. As with the logging levels, the letter case is not significant1.

The letter case is not significant for the logging level names; e.g., debug, DEBUG, and dEbuG all represent the same logging level. For consistency, our convention is to use the lower case names. Where our docs do use other forms, they do so in the context of specific examples, so you won’t be surprised if you see similar usage in the wild.

As the log level for a module is optional, the module to enable logging for is also optional. If only a level is provided, then the global log level for all modules is set to this value.

Some examples of valid values of RUST_LOG are:

Filtering results

A RUST_LOG directive may include a regex filter. The syntax is to append / followed by a regex. Each message is checked against the regex, and is only logged if it matches. Note that the matching is done after formatting the log string but before adding any logging meta-data. There is a single filter for all modules.

Some examples:

Capturing logs in tests

Records logged during cargo test will not be captured by the test harness by default. The Builder::is_test method can be used in unit tests to ensure logs will be captured:

mod tests {
    fn init() {
        let _ = env_logger::builder().is_test(true).try_init();

    fn it_works() {

        info!("This record will be captured by `cargo test`");

        assert_eq!(2, 1 + 1);

Enabling test capturing comes at the expense of color and other style support and may have performance implications.

Disabling colors

Colors and other styles can be configured with the RUST_LOG_STYLE environment variable. It accepts the following values:

Tweaking the default format

Parts of the default format can be excluded from the log output using the Builder. The following example excludes the timestamp from the log output:


Stability of the default format

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.

If you want to capture or interpret the output of env_logger programmatically then you should use a custom format.

Using a custom format

Custom formats can be provided as closures to the Builder. These closures take a Formatter and log::Record as arguments:

use std::io::Write;

    .format(|buf, record| {
        writeln!(buf, "{}: {}", record.level(), record.args())

See the fmt module for more details about custom formats.

Specifying defaults for environment variables

env_logger can read configuration from environment variables. If these variables aren’t present, the default value to use can be tweaked with the Env type. The following example defaults to log warn and above if the RUST_LOG environment variable isn’t set:

use env_logger::Env;


  1. Similar to the universe of log level names, the off pseudo log level feature is also provided by the underlying log crate. 


pub use super::TimestampPrecision;



Filtering for log records.


Formatting for log records.



Builder acts as builder for initializing a Logger.


Set of environment variables to configure from.


The env logger.



Log target, either stdout or stderr.


Whether or not to print styles to the target.



The default name for the environment variable to read filters from.


The default name for the environment variable to read style preferences from.



Create a new builder with the default environment variables.


Create a builder from the given environment variables.


Initializes the global logger with an env logger.


Initializes the global logger with an env logger from the given environment variables.


Attempts to initialize the global logger with an env logger.


Attempts to initialize the global logger with an env logger from the given environment variables.