log-wrap

Crates.iolog-wrap
lib.rslog-wrap
version0.1.0
sourcesrc
created_at2023-01-08 08:06:02.635941
updated_at2023-01-08 08:06:02.635941
descriptionUtilities for log by wrapping `Box`, such as log filtering, log capturing
homepage
repository
max_upload_size
id753522
size8,211
metaworm (metaworm)

documentation

https://docs.rs/log-wrap

README

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()
    }
}
Commit count: 0

cargo fmt