fstdout-logger 0.2.2

An implementation of the log crate that logs to stdout and to an optional log file with configurable options.
Documentation
// This example demonstrates module-level log filtering with RUST_LOG.
//
// Features shown:
// - Using RUST_LOG environment variable to control log levels per module
// - Filtering logs from different modules at different levels
// - Using init_logger_from_env() to automatically configure from RUST_LOG
//
// Try running with different RUST_LOG values:
// - RUST_LOG=debug cargo run --example module_filters
// - RUST_LOG=trace cargo run --example module_filters
// - RUST_LOG=module_filters=debug cargo run --example module_filters
// - RUST_LOG=module_filters::network=trace,module_filters::database=warn cargo run --example module_filters

use fstdout_logger::init_logger_from_env;
use log::{debug, error, info, trace, warn};

// Simulated modules
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() {
    // Initialize logger from RUST_LOG environment variable
    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");

    // Main application logs
    info!("Application starting...");
    debug!("Debug mode enabled");
    trace!("This is a trace message from main");

    // Network module operations
    info!("Testing network module:");
    network::connect();
    network::send_data();

    // Database module operations
    info!("\nTesting database module:");
    database::query();
    database::transaction();

    // Cache module operations
    info!("\nTesting cache module:");
    cache::get("user:123");
    cache::set("user:123");

    // More main application logs
    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.");
}