env_logger is a simple logger that can be configured via environment variables, for use with the logging facade exposed by the
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
By default, env_logger can be depended on with:
RUST_LOG=error cargo run
RUST_LOG=info cargo run
RUST_LOG=debug cargo run
You can also set the log level on a per module basis:
RUST_LOG=main=info cargo run
And enable all logging:
RUST_LOG=main cargo run
If the binary name contains hyphens, you will need to replace them with underscores:
RUST_LOG=my_app cargo run
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 - https://docs.rs/log/ for more information about its API.
Log levels are controlled on a per-module basis, and by default all logging is disabled except for the
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 log target is typically equal to the path of the module the message in question originated from, though it can be overriden.
The path is rooted in the name of the crate it was compiled for, so if your program is in a file called, for example,
hello.rs, the path would simply be be
Furthermore, the the log can be filtered using prefix-search based on the specified log target. A value of, for example,
RUST_LOG=example, would match all of the messages with targets:
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:
hello turns on all logging for the ‘hello’ module
trace turns on all logging for the application, regardless of its name
TRACE turns on all logging for the application, regardless of its name (same as previous)
info turns on all info logging
INFO turns on all info logging (same as previous)
hello=debug turns on debug logging for ‘hello’
hello=DEBUG turns on debug logging for ‘hello’ (same as previous)
hello,std::option turns on hello, and std’s option logging
error,hello=warn turn on global error logging and also warn for hello
error,hello=off turn on global error logging, but turn off logging for hello
off turns off all logging for the application
OFF turns off all logging for the application (same as previous)
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.
hello/foo turns on all logging for the ‘hello’ module where the log message includes ‘foo’.
info/f.o turns on all info logging where the log message includes ‘foo’, ‘f1o’, ‘fao’, etc.
hello=debug/foo*foo turns on debug logging for ‘hello’ where the log message includes ‘foofoo’ or ‘fofoo’ or ‘fooooooofoo’, etc.
error,hello=warn/[0-9]scopes turn on global error logging and also warn for hello. In both cases the log message must include a single digit number followed by ‘scopes’.
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:
Enabling test capturing comes at the expense of color and other style support and may have performance implications.
Colors and other styles can be configured with the
RUST_LOG_STYLE environment variable. It accepts the following values:
auto (default) will attempt to print style characters, but don’t force the issue. If the console isn’t available on Windows, or if TERM=dumb, for example, then don’t print colors.
always will always print style characters even if they aren’t supported by the terminal. This includes emitting ANSI colors on Windows if the console API is unavailable.
never will never print style characters.
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:
See the fmt - https://docs.rs/env_logger/0.9.0/env_logger/fmt/index.html 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:
Similar to the universe of log level names, the off pseudo log level feature is also provided by the underlying log crate. ↩