gpt4 book ai didi

logging - 如何使用log4rs创建自定义过滤器类型?

转载 作者:行者123 更新时间:2023-12-03 11:47:22 26 4
gpt4 key购买 nike

唯一可用的过滤器是级别阈值,但要说我想按特定的target: $expr过滤。如何以编程方式(或在yml文件中)让log4rs知道我需要其他类型的过滤器?我是否需要添加自定义功能来适应实现过滤器特征的新过滤器类型的添加?
我已经尝试按照documentation of the threshold filter type中的源代码使用所有关联的实现来实现自己的过滤器类型。我什至在- kind:配置文件的过滤器的.yml部分中指定了自定义过滤器名称。 log4rs还需要实现什么呢?
另外,我在运行let handle = log4rs::load_config_file("../log4rs.yml", Default::default()).unwrap();时收到以下错误
log4rs:附加到附加程序custom_filter_appender的反序列化过滤器错误:没有注册类型custom_filter的过滤器反序列化器
即使我已经为上述自定义过滤器明确定义了反序列化器。

最佳答案

有几个要回答这个问题的地方:

  • 首先,引用通过target: $expr进行过滤,您可以仅创建一个新的记录器,该记录器与要过滤的目标具有相同的名称,从而摆脱困境。这是因为log4rs仅将log crate 中的宏发送到与记录器名称具有相同目标字符串的记录器。因此,这里有一种内置的过滤器。
  • 其次,关于需要自定义功能,您不需要一个。我将在下面显示的所有内容都不需要运行自定义功能。
  • 第三,也是最有趣的是创建自定义过滤器的语法和形式。我将在下面详细介绍必要的修改:

  • 首先配置log4rs.yml文件:
     appenders:
    custom_filter_appender:
    kind: file
    path: "log/custom_filter.log"
    filters:
    - kind: custom_filter
    interesting_field: interesting_value
    encoder:
    pattern: "{d} {m}{n}"
    root:
    level: error
    loggers:
    filter_logger:
    level: <level you want to filter by>
    appenders:
    - custom_filter_appender
    additive: false
    我在上面所做的操作说明了使用配置文件使用 log4rs配置自定义过滤器的必要条件。请注意,过滤器已附加到附加程序,附加器已附加到记录器。这是附加事物的唯一方法。接下来,我将详细说明custom_filter.rs中用于自定义过滤器所需的trait实现,以及它们如何影响代表 custom_filter的字段。
    // First remember to include the appropriate log4rs or whatever other libraries you want in the custom_filter.rs file
    #[derive(Deserialize)]
    pub struct CustomFilterConfig {
    interesting_field: InterestingFieldType,
    }
    #[derive(Debug)]
    pub struct CustomFilter {
    interesting_field: InterestingFieldType,
    }

    impl CustomFilter {
    /// Creates a new `CustomFilter` with the specified interesting field.
    pub fn new(interesting_field: InterestingFieldType,) -> CustomFilter {
    CustomFilter { interesting_field }
    }
    }

    impl Filter for CustomFilter {
    fn filter(&self, record: &Record) -> Response {
    if <Some comparison about self.interesting_field> {
    Response::Accept
    } else {
    Response::Reject
    }
    }
    }

    pub struct CustomFilterDeserializer;

    impl Deserialize for CustomFilterDeserializer {
    type Trait = dyn Filter;

    type Config = CustomFilterConfig;

    fn deserialize(
    &self,
    config: CustomFilterConfig,
    _: &Deserializers,
    ) -> Result<Box<dyn Filter>, Box<dyn Error + Sync + Send>> {
    Ok(Box::new(CustomFilter::new(config.interesting_field)))
    }
    }
    如果 log4rs希望能够识别并运行过滤器,则必须实现所有这些功能。请记住,只有与名称 log具有相同目标的 filter_logger crate 宏(在这种情况下)才会进入此过滤器进行过滤。现在,要完成此操作,我们需要知道如何从main.rs中的文件中设置 log4rs配置,并添加正确的过滤器,以便可以使用它。
    let mut custom_filter_deserializer = log4rs::file::Deserializers::new();
    custom_filter_deserializer.insert(
    "custom_filter",
    crate::custom_filter::CustomFilterDeserializer,
    );
    log4rs::init_file("log4rs.yml", custom_filter_deserializer).unwrap();
    这应该是您使用 log4rs自己配置自定义过滤器所需的所有内容。

    关于logging - 如何使用log4rs创建自定义过滤器类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64862936/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com