#filter #logger #body #format #filtering

filter-logger

A simple filtering logger which will filter based on module-path

3 releases (breaking)

0.3.0 May 31, 2019
0.2.0 May 20, 2019
0.1.0 Dec 25, 2018

#28 in #body

Download history 244/week @ 2024-07-19 136/week @ 2024-07-26 68/week @ 2024-08-02 108/week @ 2024-08-09 92/week @ 2024-08-16 45/week @ 2024-08-23 29/week @ 2024-08-30 52/week @ 2024-09-06 17/week @ 2024-09-13 47/week @ 2024-09-20 27/week @ 2024-09-27 38/week @ 2024-10-04 6/week @ 2024-10-11 34/week @ 2024-10-18 45/week @ 2024-10-25 20/week @ 2024-11-01

112 downloads per month

MIT license

7KB
112 lines

Filter Logger for Rust

This simple logger will filter based on either the module path or the log body in each log line, or both.

to use, simply add the crate to your Cargo.toml:

[dependencies]
filter-logger = "*"
log = "*"

In your source code, initialize the logger with the module filter and body filter (use empty vector for no filter):

extern crate filter_logger;
#[macro_use] extern crate log;

use filter_logger::FilterLogger;

#[test]
fn test() {
    FilterLogger::init(log::Level::Info, vec!["foo2".to_string()], vec!["DON'T PRINT".to_string()]);
    foo1::log_it();
    foo2::log_it();
}

mod foo1 {
    pub fn log_it() {
        info!("This will print out");
        info!("DON'T PRINT - This will NOT print out");
    }
}

mod foo2 {
    pub fn log_it() {
        info!("This will NOT print out");
    }
}

The output will be:

<date> <time> INFO [test_log::foo1] This will print out

The filters are a simple string check on either the module_path() or the args() parameter of the record object passed into the log() function.

Date-time Format

The format can be changed by using the with_format function instead of init:

extern crate filter_logger;
#[macro_use] extern crate log;

use filter_logger::FilterLogger;

#[test]
fn test() {
    FilterLogger::with_format(log::Level::Info, vec![], vec![], "%Y%m%dT%H%M%S%z");
    info!("Should use RFC 3339 format!");
}

Dependencies

~1.5MB
~20K SLoC