log_level_numeric/
log_level_numeric.rs

1// This example demonstrates using LOG_LEVEL environment variable with numeric values
2// and the filter_module() method to suppress logs from specific modules.
3//
4// Try running with different LOG_LEVEL values:
5// - LOG_LEVEL=0 cargo run --example log_level_numeric  (Off - no logs)
6// - LOG_LEVEL=1 cargo run --example log_level_numeric  (Error only)
7// - LOG_LEVEL=2 cargo run --example log_level_numeric  (Warn and above)
8// - LOG_LEVEL=3 cargo run --example log_level_numeric  (Info and above)
9// - LOG_LEVEL=4 cargo run --example log_level_numeric  (Debug and above)
10// - LOG_LEVEL=5 cargo run --example log_level_numeric  (Trace - all logs)
11
12use fstdout_logger::{LoggerConfig, init_logger_with_config};
13use log::{LevelFilter, debug, error, info, trace, warn};
14
15// Simulated noisy modules (like rustls)
16mod noisy_library {
17    use log::{debug, info, warn};
18
19    pub fn do_work() {
20        debug!("noisy_library: Starting work (you shouldn't see this)");
21        info!("noisy_library: Processing data (you shouldn't see this)");
22        warn!("noisy_library: Minor issue detected (you shouldn't see this)");
23    }
24}
25
26fn main() {
27    // Read LOG_LEVEL from environment
28    let log_level = match std::env::var("LOG_LEVEL") {
29        Ok(level) => match level.parse::<u8>() {
30            Ok(level) => match level {
31                0 => LevelFilter::Off,
32                1 => LevelFilter::Error,
33                2 => LevelFilter::Warn,
34                3 => LevelFilter::Info,
35                4 => LevelFilter::Debug,
36                _ => LevelFilter::Trace,
37            },
38            Err(_) => LevelFilter::Warn,
39        },
40        Err(_) => LevelFilter::Warn,
41    };
42
43    // Configure logger with module filters
44    if let Err(e) = init_logger_with_config(
45        Some("log_level_numeric.log"),
46        LoggerConfig::builder()
47            .show_file_info(true)
48            .level(log_level)
49            .filter_module("log_level_numeric::noisy_library", LevelFilter::Error) // Only errors from noisy_library
50            .build(),
51    ) {
52        eprintln!("Failed to initialize logger: {}", e);
53        std::process::exit(1);
54    }
55
56    println!("=== LOG_LEVEL Numeric Example ===");
57    println!(
58        "Current LOG_LEVEL: {:?}",
59        std::env::var("LOG_LEVEL").unwrap_or_else(|_| "not set (using Warn)".to_string())
60    );
61    println!("Effective level: {:?}", log_level);
62    println!("\nLOG_LEVEL values:");
63    println!("  0 = Off");
64    println!("  1 = Error");
65    println!("  2 = Warn");
66    println!("  3 = Info");
67    println!("  4 = Debug");
68    println!("  5+ = Trace");
69    println!("\n--- Application Logs ---\n");
70
71    // Log messages at different levels
72    trace!("This is a TRACE message (level 5+)");
73    debug!("This is a DEBUG message (level 4)");
74    info!("This is an INFO message (level 3)");
75    warn!("This is a WARN message (level 2)");
76    error!("This is an ERROR message (level 1)");
77
78    // Call noisy library (its logs should be suppressed)
79    println!("\nCalling noisy_library (its logs are filtered to Error only):");
80    noisy_library::do_work();
81
82    println!("\n--- End of Logs ---");
83    println!("\nNote: Logs from 'noisy_library' are filtered to Error level only,");
84    println!("so you should not see its debug, info, or warn messages.");
85}