什么是Filter
Filter就是用来过滤日志事件的,控制日志事件应不应该被输出。Logback classic
模块主要有两类Filter
, 分别是regular filtesr
和 turbo filters
。
Filter接口内部最主要的方法就是FilterReply decide(E event)
,它决定了日志事件是否被过滤。
package ch.qos.logback.core.filter;
import ch.qos.logback.core.spi.ContextAwareBase;
import ch.qos.logback.core.spi.FilterReply;
import ch.qos.logback.core.spi.LifeCycle;
public abstract class Filter<E> extends ContextAwareBase implements LifeCycle {
/**
* If the decision is <code>{@link FilterReply#DENY}</code>, then the event will be
* dropped. If the decision is <code>{@link FilterReply#NEUTRAL}</code>, then the next
* filter, if any, will be invoked. If the decision is
* <code>{@link FilterReply#ACCEPT}</code> then the event will be logged without
* consulting with other filters in the chain.
*
* @param event
* The event to decide upon.
*/
public abstract FilterReply decide(E event);
}
注释很清楚,当返回FilterReply.DENY
日志事件被过滤,返回FilterReply.NEUTRAL
将保持中立,由Filter链上的下一个Filter进行判断,当返回FilterReply.ACCEPT
时,将输出日志事件。
Regular Filtesr
LevelFilter
LevelFilter根据精确的级别匹配来过滤事件。 如果事件的级别等于已配置的级别,则过滤器将接受或拒绝事件,具体取决于onMatch和onMismatch属性的配置。示例如下:
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>
%-4relative [%thread] %-5level %logger{30} - %msg%n
</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
ThredsholdFilter
ThresholdFilter过滤低于指定阈值的事件。 对于等于或高于阈值的事件,ThresholdFilter将在调用define()方法时响应NEUTRAL。 但是,级别低于阈值的事件将被拒绝。 示例:
<configuration>
<appender name="CONSOLE"
class="ch.qos.logback.core.ConsoleAppender">
<!-- deny all events with a level below INFO, that is TRACE and DEBUG -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<encoder>
<pattern>
%-4relative [%thread] %-5level %logger{30} - %msg%n
</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
Matcher
尽管可以通过调用String类中的matchs()方法进行模式匹配,但这会导致每次调用过滤器时都要编译一个全新的Pattern对象。 为了消除这种开销,您可以预定义一个或多个Matcher对象。 定义匹配器后,就可以在评估表达式中按名称重复引用它。
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<matcher>
<Name>odd</Name>
<!-- filter out odd numbered statements -->
<regex>statement [13579]</regex>
</matcher>
<expression>odd.matches(formattedMessage)</expression>
</evaluator>
<OnMismatch>NEUTRAL</OnMismatch>
<!-- 日志中如有statement 1,日志将被过滤 -->
<OnMatch>DENY</OnMatch>
</filter>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
TurboFilters
总体而言,它们的工作方式与前面提到的过滤器非常相似。 但是,Filter和TurboFilter对象之间有两个主要区别。
一, TurboFilter对象绑定到日志记录上下文。 因此,它们不仅在使用给定的Appender
时被调用,而且在每次发出日志记录请求时都被调用。 它们的范围比附加附加过滤器的范围大。
二,它们在LoggingEvent对象创建之前被调用。 TurboFilter对象不需要实例化日志记录事件即可过滤日志记录请求。 因此,Turbo过滤器旨在在事件创建之前就对日志事件进行高性能过滤。
示例
<configuration>
<turboFilter class="ch.qos.logback.classic.turbo.MDCFilter">
<MDCKey>username</MDCKey>
<Value>sebastien</Value>
<OnMatch>ACCEPT</OnMatch>
</turboFilter>
<turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
<Marker>billing</Marker>
<OnMatch>DENY</OnMatch>
</turboFilter>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="console" />
</root>
</configuration>
测试java类
输出结果如下: