use fstdout_logger::{LoggerConfig, init_logger_with_config};
use log::{LevelFilter, debug, error, info, trace, warn};
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() {
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,
};
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) .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");
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)");
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.");
}