Crate log_wrap

Source
Expand description

§log-wrap

Utilities for log by wrapping Box<dyn log::Log>, such as log filtering, log capturing

§Example

filter log by module name

fn main() {
    std::env::set_var("RUST_LOG", "info");
    let logger = env_logger::Builder::from_default_env().build();
    log::set_max_level(logger.filter());
    log_wrap::LogWrap::new(Box::new(logger))
        .black_module(["hyper", "h2"])
        .init()
        .unwrap();

    // your code ...
}

capturing logs in each thread, and output them synchronously before thread exiting

fn main() {
    std::env::set_var("RUST_LOG", "info");
    let logger = env_logger::Builder::from_default_env().build();
    log::set_max_level(logger.filter());
    log_wrap::LogWrap::new(Box::new(logger))
        .enable_thread_capture()
        .init()
        .unwrap();

    let joins = (0..5)
        .map(|t| {
            std::thread::spawn(move || {
                let _guard = log_wrap::capture_thread_log();
                for i in 0..5 {
                    log::info!("thread#{t} {i} ...");
                }
            })
        })
        .collect::<Vec<_>>();
    for j in joins {
        j.join().unwrap()
    }
}

Structs§

CaptureGuard
LogInfo
LogWrap
log wrapper

Functions§

capture_thread_log
Capture subsequent logs in current thread, and output them when CaptureGuard dropping