Logging in Rust with the log crate and env_logger

The log crate is Rust’s standard logging facade. It gives you the trace!, debug!, info!, warn! and error! macros, but doesn’t print anything by itself. A separate logger implementation decides where messages go and which ones are shown. That split is the point: libraries depend only on log, and the application picks one logger for everything.
This post uses env_logger, the simplest logger, which writes to stderr and is configured through the RUST_LOG environment variable.
cargo new log_examplecd log_exampleAdd both crates to Cargo.toml:
[dependencies]log = "0.4"env_logger = "0.11"env_logger has to go under [dependencies], not [dev-dependencies]: main.rs uses it, and dev-dependencies are only available to tests, examples, and benchmarks.
Initialising the logger
Section titled “Initialising the logger”src/main.rs:
use env_logger::Env;use log::{debug, error, info, trace, warn};
fn main() { // Use RUST_LOG if it is set; otherwise show `info` and above. env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
info!("Logger initialized!");
trace!("This is a trace log!"); debug!("This is a debug log!"); info!("This is an info log!"); warn!("This is a warning log!"); error!("This is an error log!");}Initialise the logger once, as early as possible in main. Any log calls before that are dropped. If you don’t need a default level, env_logger::init() is the one-line version; without RUST_LOG set, it shows only error messages.
The levels, from most to least verbose:
trace!: very detailed, step-by-step output.debug!: information useful while debugging.info!: normal progress and status messages.warn!: something unexpected that the program can recover from.error!: an operation failed.
Running it
Section titled “Running it”cargo runWith the default filter of info, the trace and debug lines are hidden. The output looks like this (the timestamps will differ):
[2023-06-02T13:00:00Z INFO log_example] Logger initialized![2023-06-02T13:00:00Z INFO log_example] This is an info log![2023-06-02T13:00:00Z WARN log_example] This is a warning log![2023-06-02T13:00:00Z ERROR log_example] This is an error log!Each line shows the time, the level, and the module the message came from.
Changing the level with RUST_LOG
Section titled “Changing the level with RUST_LOG”To show debug and above:
RUST_LOG=debug cargo runIn PowerShell, set the variable first:
$env:RUST_LOG = "debug"; cargo runRUST_LOG also accepts per-module filters, which is what makes it useful in bigger programs. Dependencies that use log are filtered the same way:
# trace for this crate, warnings and errors only for everything elseRUST_LOG=warn,log_example=trace cargo runGotchas
Section titled “Gotchas”- The macros skip formatting their arguments when the level is disabled, but code that runs before the macro (building a large debug string, say) still runs. Guard it with
if log::log_enabled!(log::Level::Debug) { ... }. env_logger’s default output format isn’t guaranteed to stay the same between versions. If something parses your logs, set a format withBuilder::format.- For async code or structured fields (request IDs, spans across
.awaitpoints), look attracinginstead. It can also capture messages from crates that uselog.
Further reading
Section titled “Further reading”- log crate docs
- env_logger docs, including the full
RUST_LOGsyntax
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)