module_filters/
module_filters.rs

1// This example demonstrates module-level log filtering with RUST_LOG.
2//
3// Features shown:
4// - Using RUST_LOG environment variable to control log levels per module
5// - Filtering logs from different modules at different levels
6// - Using init_logger_from_env() to automatically configure from RUST_LOG
7//
8// Try running with different RUST_LOG values:
9// - RUST_LOG=debug cargo run --example module_filters
10// - RUST_LOG=trace cargo run --example module_filters
11// - RUST_LOG=module_filters=debug cargo run --example module_filters
12// - RUST_LOG=module_filters::network=trace,module_filters::database=warn cargo run --example module_filters
13
14use fstdout_logger::init_logger_from_env;
15use log::{debug, error, info, trace, warn};
16
17// Simulated modules
18mod network {
19    use log::{debug, info, trace};
20
21    pub fn connect() {
22        trace!("Attempting to establish connection...");
23        debug!("Opening socket on port 8080");
24        info!("Connected to server");
25    }
26
27    pub fn send_data() {
28        trace!("Preparing data packet");
29        debug!("Serializing payload");
30        info!("Data sent successfully");
31    }
32}
33
34mod database {
35    use log::{debug, info, trace, warn};
36
37    pub fn query() {
38        trace!("Building SQL query");
39        debug!("Executing: SELECT * FROM users");
40        info!("Query returned 42 rows");
41    }
42
43    pub fn transaction() {
44        trace!("Starting transaction");
45        debug!("Acquiring lock");
46        warn!("Transaction took longer than expected");
47        info!("Transaction committed");
48    }
49}
50
51mod cache {
52    use log::{debug, info, trace};
53
54    pub fn get(key: &str) {
55        trace!("Looking up key: {}", key);
56        debug!("Cache miss for key: {}", key);
57        info!("Fetching from backend");
58    }
59
60    pub fn set(key: &str) {
61        trace!("Computing cache key hash");
62        debug!("Writing to cache: {}", key);
63        info!("Cache updated");
64    }
65}
66
67fn main() {
68    // Initialize logger from RUST_LOG environment variable
69    if let Err(e) = init_logger_from_env(Some("module_filters.log")) {
70        eprintln!("Failed to initialize logger: {e}");
71        return;
72    }
73
74    println!("=== Module Filters Example ===");
75    println!("Current RUST_LOG: {:?}", std::env::var("RUST_LOG").unwrap_or_else(|_| "not set".to_string()));
76    println!("\nTry running with different RUST_LOG values:");
77    println!("  RUST_LOG=debug cargo run --example module_filters");
78    println!("  RUST_LOG=trace cargo run --example module_filters");
79    println!("  RUST_LOG=module_filters::network=trace,warn cargo run --example module_filters");
80    println!("  RUST_LOG=module_filters::database=error cargo run --example module_filters");
81    println!("\n--- Application Logs ---\n");
82
83    // Main application logs
84    info!("Application starting...");
85    debug!("Debug mode enabled");
86    trace!("This is a trace message from main");
87
88    // Network module operations
89    info!("Testing network module:");
90    network::connect();
91    network::send_data();
92
93    // Database module operations
94    info!("\nTesting database module:");
95    database::query();
96    database::transaction();
97
98    // Cache module operations
99    info!("\nTesting cache module:");
100    cache::get("user:123");
101    cache::set("user:123");
102
103    // More main application logs
104    warn!("This is a warning from main");
105    error!("This is an error from main");
106
107    info!("\nApplication finished");
108    println!("\n--- End of Logs ---");
109    println!("\nCheck 'module_filters.log' for complete log output.");
110}