#env-var #logging #log #logger

env_filter

Filter log events using environment variables

3 releases

0.1.2 Jul 25, 2024
0.1.1 Jul 23, 2024
0.1.0 Jan 19, 2024

#143 in Debugging

Download history 334735/week @ 2024-07-14 357959/week @ 2024-07-21 386447/week @ 2024-07-28 375246/week @ 2024-08-04 438772/week @ 2024-08-11 419171/week @ 2024-08-18 412400/week @ 2024-08-25 415940/week @ 2024-09-01 434674/week @ 2024-09-08 429935/week @ 2024-09-15 482180/week @ 2024-09-22 488186/week @ 2024-09-29 516538/week @ 2024-10-06 539482/week @ 2024-10-13 534834/week @ 2024-10-20 495100/week @ 2024-10-27

2,121,107 downloads per month
Used in 2,300 crates (9 directly)

MIT/Apache

48KB
971 lines

env_filter

crates.io Documentation

Filter log events using environment variables


lib.rs:

Filtering for log records.

You can use the Filter type in your own logger implementation to use the same filter parsing and matching as env_logger.

Using env_filter in your own logger

You can use env_filter's filtering functionality with your own logger. Call Builder::parse to parse directives from a string when constructing your logger. Call Filter::matches to check whether a record should be logged based on the parsed filters when log records are received.

use env_filter::Filter;
use log::{Log, Metadata, Record};

struct PrintLogger;

impl Log for PrintLogger {
    fn enabled(&self, metadata: &Metadata) -> bool {
        true
    }

    fn log(&self, record: &Record) {
        println!("{:?}", record);
    }

    fn flush(&self) {}
}

let mut builder = env_filter::Builder::new();
// Parse a directives string from an environment variable
if let Ok(ref filter) = std::env::var("MY_LOG_LEVEL") {
    builder.parse(filter);
}

let logger = env_filter::FilteredLog::new(PrintLogger, builder.build());

Dependencies

~2.7–4MB
~68K SLoC