use fstdout_logger::init_logger_from_env;
use log::{debug, error, info, trace, warn};
mod network {
use log::{debug, info, trace};
pub fn connect() {
trace!("Attempting to establish connection...");
debug!("Opening socket on port 8080");
info!("Connected to server");
}
pub fn send_data() {
trace!("Preparing data packet");
debug!("Serializing payload");
info!("Data sent successfully");
}
}
mod database {
use log::{debug, info, trace, warn};
pub fn query() {
trace!("Building SQL query");
debug!("Executing: SELECT * FROM users");
info!("Query returned 42 rows");
}
pub fn transaction() {
trace!("Starting transaction");
debug!("Acquiring lock");
warn!("Transaction took longer than expected");
info!("Transaction committed");
}
}
mod cache {
use log::{debug, info, trace};
pub fn get(key: &str) {
trace!("Looking up key: {}", key);
debug!("Cache miss for key: {}", key);
info!("Fetching from backend");
}
pub fn set(key: &str) {
trace!("Computing cache key hash");
debug!("Writing to cache: {}", key);
info!("Cache updated");
}
}
fn main() {
if let Err(e) = init_logger_from_env(Some("module_filters.log")) {
eprintln!("Failed to initialize logger: {e}");
return;
}
println!("=== Module Filters Example ===");
println!("Current RUST_LOG: {:?}", std::env::var("RUST_LOG").unwrap_or_else(|_| "not set".to_string()));
println!("\nTry running with different RUST_LOG values:");
println!(" RUST_LOG=debug cargo run --example module_filters");
println!(" RUST_LOG=trace cargo run --example module_filters");
println!(" RUST_LOG=module_filters::network=trace,warn cargo run --example module_filters");
println!(" RUST_LOG=module_filters::database=error cargo run --example module_filters");
println!("\n--- Application Logs ---\n");
info!("Application starting...");
debug!("Debug mode enabled");
trace!("This is a trace message from main");
info!("Testing network module:");
network::connect();
network::send_data();
info!("\nTesting database module:");
database::query();
database::transaction();
info!("\nTesting cache module:");
cache::get("user:123");
cache::set("user:123");
warn!("This is a warning from main");
error!("This is an error from main");
info!("\nApplication finished");
println!("\n--- End of Logs ---");
println!("\nCheck 'module_filters.log' for complete log output.");
}