一个附加器中有多个过滤器

时间:2019-09-18 13:00:58

标签: logging logback

我可以在logback.xml的一个附加程序下配置多个日志过滤器吗?

我尝试如下创建,但这不起作用。有办法使它起作用吗?

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="FilterClass1">
</filter>

<filter class="FilterClass2">
</filter>
</appender>

2 个答案:

答案 0 :(得分:0)

在要匹配的第一个过滤条件中:FilterReply.NEUTRAL

    public class FirstFilter extends Filter {
    @Override
    public FilterReply decide(Object eventObject) {
        LoggingEvent event = (LoggingEvent) eventObject;
        if (event.getMessage() != null && event.getMessage().contains("sampel")) {
            return FilterReply.NEUTRAL;
        } else {
            return FilterReply.DENY;
        }
    }
}

,并在第二个过滤器类中设置FilterReply.ACCEPT

public class SecondFilter extends Filter {
    @Override
    public FilterReply decide(Object event) {
        LoggingEvent event1 = (LoggingEvent) event;
        if (event1.getMessage() != null && event1.getMessage().contains("sample2")) {
            return FilterReply.ACCEPT;
        } else {
            return FilterReply.DENY;
        }
    }
}

答案 1 :(得分:0)

以logback.xml的术语添加到Najme的答案中

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  <filter class="FilterClass1">
  <onMatch>NEUTRAL</onMatch>
  <onMismatch>DENY</onMismatch>
</filter>          


<filter class="FilterClass2">
  <onMatch>ACCEPT</onMatch>
  <onMismatch>DENY</onMismatch>
</filter>
</appender>

第一个过滤器应该onMatch(),设置为NEUTRAL,随后的过滤器... 仅将最终过滤器onMatch()设置为ACCEPT。