#log #wrapper #logging #logger

log_wrapper

Just some boilerplate code to a logger wrapper over the log crate that logs to STDOUT

2 releases

0.1.1 Feb 22, 2020
0.1.0 Feb 22, 2020

#560 in Debugging

MIT/Apache

7KB

log_wrapper

docs crates.io

Just some boilerplate code for a logger wrapped over the log crate that logs to STDOUT. This has a global static ConsoleLogger struct that over-rides the log::Log trait. Once this is initiated with the ConsoleLogger::new(loglevel), where loglevel is either of trace, debug, info, warn, error, off, the standard macros from log crate such as info!, warn!, etc. can be used to print and the output will be filtered as per the defined loglevel.

Documentation

Eg:

somemod/src/main.rc

#[macro_use]
extern crate log;
use log_wrapper::{ConsoleLogger};
use somemod_backend::print_log;
fn main() {
    ConsoleLogger::new("warn".to_string());
    info!("This is info");
    warn!("warning");
    error!("oops");
    println!("Now from module");
    print_log();

}

somemod_backend/src/lib.rs

#[macro_use]
extern crate log;
pub mod nested_mod{
    pub fn print_log() {
        info!("This is info");
        warn!("warning");
        error!("oops");
    }
}

Output:

23/02/2020:02:29:10 WARN module:somemod file:somemod/src/main.rs:8 warning
23/02/2020:02:29:10 ERROR module:somemod file:somemod/src/main.rs:9 oops
Now from module
23/02/2020:02:29:10 WARN module:somemod_backend::nested_mod file:somemod_backend/src/lib.rs:6 warning
23/02/2020:02:29:10 ERROR module:somemod_backend::nested_mod file:somemod_backend/src/lib.rs:7 oops

Why

This is just the boilerplate code taken from the log crate itself that I wanted to use so that I don't have to type it every time.

Dependencies

~1.5MB
~20K SLoC