log_level_numeric/
log_level_numeric.rs1use fstdout_logger::{LoggerConfig, init_logger_with_config};
13use log::{LevelFilter, debug, error, info, trace, warn};
14
15mod 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 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 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) .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 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 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}