#log #logging #capturing #box-dyn #wrapping #filtering #wrap

nightly no-std log-wrap

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

1 unstable release

0.1.0 Jan 8, 2023

#5 in #box-dyn

MIT license

9KB
163 lines

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()
    }
}

Dependencies

~87KB