一、Logback中Filter的种类
在logback-classic中,提供了两种类型的filter:regular过滤器和turbo过滤器。
1、Regular过滤器
规律型过滤器继承自Filter抽象类,该类主要由带有ILoggingEvent参数的decide()方法组成。多个过滤器会基于三进制规则进行排序,每个过滤器的decide(ILoggingEvent event)方法依次会被调用,该方法返回FilterReply的枚举值,例如:DENY、NEUTRAL、ACCEPT。如果方法返回值为DENY,那么日志对象会被终止而不会被其它过滤器访问。如果方法返回值为NEUTRAL,那么下一个过滤器会进行处理,如果没有其余过滤器,那么日志会被正常处理。如果方法返回值为ACCEPT,那么日志对象会跳过其余过滤器被立即处理。
1.1 自定义过滤器
package chapters.filters; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.filter.Filter; import ch.qos.logback.core.spi.FilterReply; public class SampleFilter extends Filter<ILoggingEvent> { @Override public FilterReply decide(ILoggingEvent event) { if (event.getMessage().contains("sample")) { return FilterReply.ACCEPT; } else { return FilterReply.NEUTRAL; } } }
<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <filter class="chapters.filters.SampleFilter" /> <encoder> <pattern> %-4relative [%thread] %-5level %logger - %msg%n </pattern> </encoder> </appender> <root> <appender-ref ref="STDOUT" /> </root> </configuration>
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>
ThresholdFilter会过滤低于指定阀值的日志事件,对于等于或者大于阀值的日志事件,该过滤器会响应NEUTRAL,而对于低于阀值的日志事件,改过滤器会响应DENY。如下:
<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>
1.4 其它过滤器
EvaluatorFilter:这是包装了EventEvaluator的通用过滤器,见名知义,EventEvaluator会根据指定条件是否满足对日志事件进行判断是接收还是拒绝,该过滤器是一个抽象类。
该过滤器具体实现有:
GEventEvaluator:基于Groovy语言布尔表达式作为判断条件。
JaninoEventEvaluator:基于返回布尔值的Java语言块作为判断条件,具体配置参考配置表。
示例1:GEventEvaluator
<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.core.filter.EvaluatorFilter"> <evaluator class="ch.qos.logback.classic.boolex.GEventEvaluator"> <expression> e.level.toInt() >= WARN.toInt() && <!-- Stands for && in XML --> !(e.mdc?.get("req.userAgent") =~ /Googlebot|msnbot|Yahoo/ ) </expression> </evaluator> <OnMismatch>DENY</OnMismatch> <OnMatch>NEUTRAL</OnMatch> </filter> <encoder> <pattern> %-4relative [%thread] %-5level %logger - %msg%n </pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="STDOUT" /> </root> </configuration>
<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.core.filter.EvaluatorFilter"> <evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator --> <expression>return message.contains("billing");</expression> </evaluator> <OnMismatch>NEUTRAL</OnMismatch> <OnMatch>DENY</OnMatch> </filter> <encoder> <pattern> %-4relative [%thread] %-5level %logger - %msg%n </pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="STDOUT" /> </root> </configuration>