File: filter.rs

package info (click to toggle)
rust-flexi-logger 0.29.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,084 kB
  • sloc: makefile: 2
file content (30 lines) | stat: -rw-r--r-- 789 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use flexi_logger::{
    filter::{LogLineFilter, LogLineWriter},
    DeferredNow, FlexiLoggerError,
};

fn main() -> Result<(), FlexiLoggerError> {
    flexi_logger::Logger::try_with_str("info")?
        .filter(Box::new(BarsOnly))
        .start()?;
    log::info!("barista");
    log::info!("foo"); // will be swallowed by the filter
    log::info!("bar");
    log::info!("gaga"); // will be swallowed by the filter
    Ok(())
}

pub struct BarsOnly;
impl LogLineFilter for BarsOnly {
    fn write(
        &self,
        now: &mut DeferredNow,
        record: &log::Record,
        log_line_writer: &dyn LogLineWriter,
    ) -> std::io::Result<()> {
        if record.args().to_string().contains("bar") {
            log_line_writer.write(now, record)?;
        }
        Ok(())
    }
}