Rust Logoenv_logger

env_logger is a popular logging implementation for the `log` crate in Rust. It allows developers to configure logging behavior at runtime primarily through environment variables, making it highly flexible for development, testing, and production environments without needing to recompile the application.

Key Features:

1. Environment Variable Configuration (`RUST_LOG`): Its most distinctive feature is the ability to control log levels and filters using the `RUST_LOG` environment variable. This enables granular control over what messages are logged, from which modules, and at what severity level.
* `RUST_LOG=info`: Logs all messages at INFO level and above (WARN, ERROR).
* `RUST_LOG=debug`: Logs DEBUG, INFO, WARN, ERROR messages.
* `RUST_LOG=my_crate::my_module=debug,warn`: Logs DEBUG messages from `my_crate::my_module` and WARN messages from all other modules.
* `RUST_LOG=trace,my_crate::my_module=error`: Logs TRACE messages globally but only ERROR messages from `my_crate::my_module`.
2. Integration with `log` crate: `env_logger` implements the `log::Log` trait, making it compatible with the standard `log` facade. This means you use the `log` crate's macros (e.g., `info!`, `warn!`, `error!`, `debug!`, `trace!`) in your application code, and `env_logger` handles the output.
3. Output Format: By default, it outputs log messages to `stderr` with a format that includes the timestamp, log level, target (module path), and the message itself. The format can be customized using `env_logger::builder()`.
4. Thread-safe: `env_logger` is designed to be thread-safe, ensuring correct logging behavior in multi-threaded applications.
5. Easy Initialization: It typically requires just a single call to `env_logger::init()` or `env_logger::builder().init()` early in your application's `main` function.

How to Use `env_logger`:

1. Add `log` and `env_logger` as dependencies in your `Cargo.toml`.
2. Call `env_logger::init()` (or a more configured `env_logger::builder().init()`) at the start of your `main` function.
3. Use the `log` crate's macros (e.g., `info!`, `warn!`, `error!`) throughout your application code to emit log messages.

`env_logger` simplifies logging configuration considerably, especially for applications that need flexible logging without recompilation.

Example Code

```rust
// Cargo.toml
// [dependencies]
// log = "0.4"
// env_logger = "0.11"

use log::{debug, error, info, trace, warn};

// A dummy module to demonstrate module-specific logging
mod my_module {
    use log::{debug, info};

    pub fn do_something() {
        info!("Doing something important in my_module.");
        debug!("Detailed debug info from my_module.");
    }
}

fn main() {
    // Initialize env_logger. This should typically be called once at the start of your application.
    // By default, it reads the RUST_LOG environment variable.
    env_logger::init();

    info!("Application started.");
    debug!("This is a debug message. It won't show unless RUST_LOG is set to debug or trace.");
    trace!("This is a trace message. Very verbose, only shows with RUST_LOG=trace.");

    my_module::do_something();

    warn!("Something potentially problematic happened.");
    error!("An unrecoverable error occurred!");

    info!("Application finished.");
}

/*
To run this example:

1.  Save the code as `src/main.rs` and add the dependencies to `Cargo.toml`.

    Cargo.toml:
    ```toml
    [package]
    name = "env_logger_example"
    version = "0.1.0"
    edition = "2021"

    [dependencies]
    log = "0.4"
    env_logger = "0.11"
    ```

2.  Compile and run with different `RUST_LOG` settings:

    *   No `RUST_LOG` (defaults to ERROR):
        `cargo run`
        (You will only see the ERROR message)

    *   Set `RUST_LOG` to info:
        `RUST_LOG=info cargo run`
        (You will see INFO, WARN, ERROR messages)

    *   Set `RUST_LOG` to debug:
        `RUST_LOG=debug cargo run`
        (You will see DEBUG, INFO, WARN, ERROR messages)

    *   Set `RUST_LOG` to trace:
        `RUST_LOG=trace cargo run`
        (You will see TRACE, DEBUG, INFO, WARN, ERROR messages)

    *   Filter by module (e.g., show debug for `my_module` but info for others):
        `RUST_LOG="env_logger_example::my_module=debug,info" cargo run`
        (Replace `env_logger_example` with your actual crate name from `Cargo.toml`)
        (You will see debug from `my_module` and info from `main` and all other modules)

    *   Filter by multiple patterns:
        `RUST_LOG="env_logger_example::my_module=trace,info" cargo run`
        (Shows trace from `my_module` and info from `main` and other modules)
*/
```