Crate env_logger [] [src]

A logger configured via an environment variable which writes to standard error.

Example

#[macro_use] extern crate log;
extern crate env_logger;

use log::Level;

fn main() {
    env_logger::init();

    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
ERROR:main: this is printed by default
$ RUST_LOG=info ./main
ERROR:main: this is printed by default
INFO:main: the answer was: 12
$ RUST_LOG=debug ./main
DEBUG:main: this is a debug message
ERROR:main: this is printed by default
INFO:main: the answer was: 12

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

$ RUST_LOG=main=info ./main
ERROR:main: this is printed by default
INFO:main: the answer was: 12

And enable all logging:

$ RUST_LOG=main ./main
DEBUG:main: this is a debug message
ERROR:main: this is printed by default
INFO:main: the answer was: 12

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 error!. 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:

path::to::module=level

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 hello.rs, 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.

The actual level is optional to specify. If omitted, all logging will be enabled. If specified, it must be one of the strings debug, error, info, warn, or trace.

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:

Structs

Builder

Builder acts as builder for initializing the Logger. It can be used to customize the log format, change the enviromental variable used to provide the logging directives and also set the default log level filter.

Logger

The logger.

Enums

Target

Log target, either stdout or stderr.

Functions

init

Initializes the global logger with an env logger.

try_init

Initializes the global logger with an env logger.