### Adding `env_logger` for Rust Tests Source: https://github.com/rust-cli/env_logger/blob/main/README.md These commands add `log` as a regular dependency and `env_logger` as a development dependency, which is the recommended setup for using `env_logger` within Rust tests. ```console $ cargo add log $ cargo add --dev env_logger ``` -------------------------------- ### Running Rust Executable with `RUST_LOG` (INFO level) Source: https://github.com/rust-cli/env_logger/blob/main/README.md This example illustrates that the `RUST_LOG` environment variable is case-insensitive for log level names. Setting it to `INFO` achieves the same result as `info`. ```bash $ RUST_LOG=INFO ./main ``` -------------------------------- ### Initializing `env_logger` in a Rust Executable Source: https://github.com/rust-cli/env_logger/blob/main/README.md This Rust code snippet demonstrates how to initialize `env_logger` early in the `main` function of an executable. After initialization, `log` macros like `info!` can be used to emit log messages. ```rust use log::info; fn main() { env_logger::init(); info!("starting up"); // ... } ``` -------------------------------- ### Running a Single Rust Test with Logging Source: https://github.com/rust-cli/env_logger/blob/main/README.md This command demonstrates how to run a specific Rust test (`it_adds_one`) with `RUST_LOG` enabled for the `my_lib` module, which helps in isolating and debugging individual test cases. ```bash $ RUST_LOG=my_lib=info cargo test it_adds_one ``` -------------------------------- ### Running Rust Executable with `RUST_LOG` (info level) Source: https://github.com/rust-cli/env_logger/blob/main/README.md This bash command shows how to execute a compiled Rust binary while setting the `RUST_LOG` environment variable to `info`. This configuration enables the display of log messages at the 'info' level and higher. ```bash $ RUST_LOG=info ./main ``` -------------------------------- ### Configuring `env_logger` in Rust Tests Source: https://github.com/rust-cli/env_logger/blob/main/README.md This Rust code demonstrates how to set up `env_logger` within a test module. The `init()` function ensures logging is enabled for each test by using `env_logger::builder().is_test(true).try_init()`, preventing multiple initializations. ```rust fn add_one(num: i32) -> i32 { info!("add_one called with {}", num); num + 1 } #[cfg(test)] mod tests { use super::*; use log::info; fn init() { let _ = env_logger::builder().is_test(true).try_init(); } #[test] fn it_adds_one() { init(); info!("can log from the test too"); assert_eq!(3, add_one(2)); } #[test] fn it_handles_negative_numbers() { init(); info!("logging from another test"); assert_eq!(-7, add_one(-8)); } } ``` -------------------------------- ### Running Rust Tests with Module-Specific Logging Source: https://github.com/rust-cli/env_logger/blob/main/README.md This bash command shows how to run Rust tests while filtering log messages to only display 'info' level messages from the `my_lib` module, useful for focused debugging. ```bash $ RUST_LOG=my_lib=info cargo test ``` -------------------------------- ### Adding `env_logger` and `log` to Project Dependencies Source: https://github.com/rust-cli/env_logger/blob/main/README.md This command adds the `log` and `env_logger` crates as dependencies to your Rust project, which is necessary for using `env_logger` in executables. ```console $ cargo add log env_logger ``` -------------------------------- ### Configuring `env_logger` to Log to Standard Output Source: https://github.com/rust-cli/env_logger/blob/main/README.md This Rust snippet illustrates how to use `env_logger::Builder` to explicitly set the log target to standard output (`Stdout`) instead of the default standard error (`Stderr`). ```rust use std::env; use env_logger::{Builder, Target}; let mut builder = Builder::from_default_env(); builder.target(Target::Stdout); builder.init(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.