tari_log4rs/filter/
mod.rs

1//! Filters
2
3use log::Record;
4#[cfg(feature = "config_parsing")]
5use serde::de;
6#[cfg(feature = "config_parsing")]
7use serde_value::Value;
8#[cfg(feature = "config_parsing")]
9use std::collections::BTreeMap;
10use std::fmt;
11
12#[cfg(feature = "config_parsing")]
13use crate::config::Deserializable;
14
15#[cfg(feature = "threshold_filter")]
16pub mod threshold;
17
18/// The trait implemented by log4rs filters.
19///
20/// Filters are associated with appenders and limit the log events that will be
21/// sent to that appender.
22pub trait Filter: fmt::Debug + Send + Sync + 'static {
23    /// Filters a log event.
24    fn filter(&self, record: &Record) -> Response;
25}
26
27#[cfg(feature = "config_parsing")]
28impl Deserializable for dyn Filter {
29    fn name() -> &'static str {
30        "filter"
31    }
32}
33
34/// The response returned by a filter.
35pub enum Response {
36    /// Accept the log event.
37    ///
38    /// It will be immediately passed to the appender, bypassing any remaining
39    /// filters.
40    Accept,
41
42    /// Take no action on the log event.
43    ///
44    /// It will continue on to remaining filters or pass on to the appender if
45    /// there are none remaining.
46    Neutral,
47
48    /// Reject the log event.
49    Reject,
50}
51
52/// Configuration for a filter.
53#[cfg(feature = "config_parsing")]
54#[derive(Clone, Eq, PartialEq, Hash, Debug)]
55pub struct FilterConfig {
56    /// The filter kind.
57    pub kind: String,
58    /// The filter configuration.
59    pub config: Value,
60}
61
62#[cfg(feature = "config_parsing")]
63impl<'de> de::Deserialize<'de> for FilterConfig {
64    fn deserialize<D>(d: D) -> Result<FilterConfig, D::Error>
65    where
66        D: de::Deserializer<'de>,
67    {
68        let mut map = BTreeMap::<Value, Value>::deserialize(d)?;
69
70        let kind = match map.remove(&Value::String("kind".to_owned())) {
71            Some(kind) => kind.deserialize_into().map_err(|e| e.to_error())?,
72            None => return Err(de::Error::missing_field("kind")),
73        };
74
75        Ok(FilterConfig {
76            kind,
77            config: Value::Map(map),
78        })
79    }
80}