Initialize env_logger in Rust by adding the dependency, calling env_logger::init(), and setting the RUST_LOG environment variable.
Add env_logger to your dependencies and initialize it in main before any logging calls to configure output via environment variables.
use log::*;
fn main() {
env_logger::init();
info!("Hello, world!");
}
Add this to your Cargo.toml:
[dependencies]
env_logger = "0.9.3"
log = "0.4.20"
Set the RUST_LOG environment variable to control verbosity, for example: RUST_LOG=info cargo run.
env_logger is a tool that lets you control how much detail your Rust program prints to the console without changing the code. You set a single environment variable to turn on debug messages or hide them, similar to adjusting the volume on a speaker. This is essential for troubleshooting issues in production or development environments.