fstdout-logger 0.2.2

An implementation of the log crate that logs to stdout and to an optional log file with configurable options.
Documentation
// This example demonstrates using LOG_LEVEL environment variable with numeric values
// and the filter_module() method to suppress logs from specific modules.
//
// Try running with different LOG_LEVEL values:
// - LOG_LEVEL=0 cargo run --example log_level_numeric  (Off - no logs)
// - LOG_LEVEL=1 cargo run --example log_level_numeric  (Error only)
// - LOG_LEVEL=2 cargo run --example log_level_numeric  (Warn and above)
// - LOG_LEVEL=3 cargo run --example log_level_numeric  (Info and above)
// - LOG_LEVEL=4 cargo run --example log_level_numeric  (Debug and above)
// - LOG_LEVEL=5 cargo run --example log_level_numeric  (Trace - all logs)

use fstdout_logger::{LoggerConfig, init_logger_with_config};
use log::{LevelFilter, debug, error, info, trace, warn};

// Simulated noisy modules (like rustls)
mod noisy_library {
    use log::{debug, info, warn};

    pub fn do_work() {
        debug!("noisy_library: Starting work (you shouldn't see this)");
        info!("noisy_library: Processing data (you shouldn't see this)");
        warn!("noisy_library: Minor issue detected (you shouldn't see this)");
    }
}

fn main() {
    // Read LOG_LEVEL from environment
    let log_level = match std::env::var("LOG_LEVEL") {
        Ok(level) => match level.parse::<u8>() {
            Ok(level) => match level {
                0 => LevelFilter::Off,
                1 => LevelFilter::Error,
                2 => LevelFilter::Warn,
                3 => LevelFilter::Info,
                4 => LevelFilter::Debug,
                _ => LevelFilter::Trace,
            },
            Err(_) => LevelFilter::Warn,
        },
        Err(_) => LevelFilter::Warn,
    };

    // Configure logger with module filters
    if let Err(e) = init_logger_with_config(
        Some("log_level_numeric.log"),
        LoggerConfig::builder()
            .show_file_info(true)
            .level(log_level)
            .filter_module("log_level_numeric::noisy_library", LevelFilter::Error) // Only errors from noisy_library
            .build(),
    ) {
        eprintln!("Failed to initialize logger: {}", e);
        std::process::exit(1);
    }

    println!("=== LOG_LEVEL Numeric Example ===");
    println!(
        "Current LOG_LEVEL: {:?}",
        std::env::var("LOG_LEVEL").unwrap_or_else(|_| "not set (using Warn)".to_string())
    );
    println!("Effective level: {:?}", log_level);
    println!("\nLOG_LEVEL values:");
    println!("  0 = Off");
    println!("  1 = Error");
    println!("  2 = Warn");
    println!("  3 = Info");
    println!("  4 = Debug");
    println!("  5+ = Trace");
    println!("\n--- Application Logs ---\n");

    // Log messages at different levels
    trace!("This is a TRACE message (level 5+)");
    debug!("This is a DEBUG message (level 4)");
    info!("This is an INFO message (level 3)");
    warn!("This is a WARN message (level 2)");
    error!("This is an ERROR message (level 1)");

    // Call noisy library (its logs should be suppressed)
    println!("\nCalling noisy_library (its logs are filtered to Error only):");
    noisy_library::do_work();

    println!("\n--- End of Logs ---");
    println!("\nNote: Logs from 'noisy_library' are filtered to Error level only,");
    println!("so you should not see its debug, info, or warn messages.");
}