#env-var #log #logging #logger

env_filter

Filter log events using environment variables

1 unstable release

0.1.0 Jan 19, 2024

#189 in Debugging

Download history 920/week @ 2024-01-13 40561/week @ 2024-01-20 71837/week @ 2024-01-27 86374/week @ 2024-02-03 103725/week @ 2024-02-10 103560/week @ 2024-02-17 120947/week @ 2024-02-24 146408/week @ 2024-03-02 158751/week @ 2024-03-09 195351/week @ 2024-03-16 186041/week @ 2024-03-23 213322/week @ 2024-03-30 196859/week @ 2024-04-06

818,966 downloads per month
Used in 1,033 crates (2 directly)

MIT/Apache

37KB
709 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.8–4MB
~68K SLoC