log4rc 0.3.0

A log4j-like logging framework for Rust
Documentation
//! Demonstrates the `FileAppender`: log to the console and to a file at the
//! same time via `Logger::builder`.

use std::io::Read;

use log4rc::{ConsoleAppender, FileAppender, LogLevel, Logger, SimpleLayout};

fn main() {
    let log_path = std::env::temp_dir().join("log4rc-file-appender.log");

    Logger::builder(LogLevel::Info)
        .appender(Box::new(ConsoleAppender::new(Box::new(SimpleLayout))))
        .appender(Box::new(
            FileAppender::open(&log_path, Box::new(SimpleLayout))
                .expect("failed to open log file"),
        ))
        .init();

    log4rc::info!("Writing to console and file");
    log4rc::error!("An error also goes to both");
    log4rc::debug!("Filtered out at Info level");

    // Read the file back to prove the messages were written to disk.
    let mut contents = String::new();
    std::fs::File::open(&log_path)
        .expect("failed to reopen log file")
        .read_to_string(&mut contents)
        .expect("failed to read log file");

    println!("\n--- Contents of {} ---", log_path.display());
    print!("{contents}");
    let _ = std::fs::remove_file(&log_path);
}