logback ThresholdFilter临界值日志过滤器源码解读
序
本文主要研究一下logback的ThresholdFilter
ThresholdFilter
ch/qos/logback/classic/filter/ThresholdFilter.java
public class ThresholdFilter extends Filter<ILoggingEvent> { Level level; @Override public FilterReply decide(ILoggingEvent event) { if (!isStarted()) { return FilterReply.NEUTRAL; } if (event.getLevel().isGreaterOrEqual(level)) { return FilterReply.NEUTRAL; } else { return FilterReply.DENY; } } public void setLevel(String level) { this.level = Level.toLevel(level); } public void start() { if (this.level != null) { super.start(); } } }
ThresholdFilter继承了Filter,其decide方法从ILoggingEvent获取level,若该level大于等于指定的level则返回NEUTRAL,否则返回DENY
isGreaterOrEqual
ch/qos/logback/classic/Level.java
public final class Level implements java.io.Serializable { private static final long serialVersionUID = -814092767334282137L; public static final int OFF_INT = Integer.MAX_VALUE; public static final int ERROR_INT = 40000; public static final int WARN_INT = 30000; public static final int INFO_INT = 20000; public static final int DEBUG_INT = 10000; public static final int TRACE_INT = 5000; public static final int ALL_INT = Integer.MIN_VALUE; /** * The <code>OFF</code> is used to turn off logging. */ public static final Level OFF = new Level(OFF_INT, "OFF"); /** * The <code>ERROR</code> level designates error events which may or not be * fatal to the application. */ public static final Level ERROR = new Level(ERROR_INT, "ERROR"); /** * The <code>WARN</code> level designates potentially harmful situations. */ public static final Level WARN = new Level(WARN_INT, "WARN"); /** * The <code>INFO</code> level designates informational messages highlighting * overall progress of the application. */ public static final Level INFO = new Level(INFO_INT, "INFO"); /** * The <code>DEBUG</code> level designates informational events of lower * importance. */ public static final Level DEBUG = new Level(DEBUG_INT, "DEBUG"); /** * The <code>TRACE</code> level designates informational events of very low * importance. */ public static final Level TRACE = new Level(TRACE_INT, "TRACE"); /** * The <code>ALL</code> is used to turn on all logging. */ public static final Level ALL = new Level(ALL_INT, "ALL"); public final int levelInt; public final String levelStr; /** * Instantiate a Level object. */ private Level(int levelInt, String levelStr) { this.levelInt = levelInt; this.levelStr = levelStr; } /** * Returns <code>true</code> if this Level has a higher or equal Level than the * Level passed as argument, <code>false</code> otherwise. */ public boolean isGreaterOrEqual(Level r) { return levelInt >= r.levelInt; } //...... }
Level定义了OFF(Integer.MAX_VALUE)、ERROR(40000)、WARN(30000)、INFO(20000)、DEBUG(10000)、TRACE(5000)、ALL(Integer.MIN_VALUE)这几个level,其int值依次减小。isGreaterOrEqual方法则是根据levelInt值来判断。
示例
<appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>infoLog.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>infoLogs.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> <totalSizeCap>3GB</totalSizeCap> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n </pattern> </encoder> </appender>
这里对FILE_INFO添加了ThresholdFilter,只有级别大于等于INFO的才打印
小结
logback的ThresholdFilter继承了Filter,其decide方法从ILoggingEvent获取level,若该level大于等于指定的level则返回NEUTRAL,否则返回DENY。Level定义了OFF(Integer.MAX_VALUE
)、ERROR(40000
)、WARN(30000
)、INFO(20000
)、DEBUG(10000
)、TRACE(5000
)、ALL(Integer.MIN_VALUE
)这几个level,其int值依次减小。isGreaterOrEqual方法则是根据levelInt值来判断。
以上就是logback ThresholdFilter临界值日志过滤器源码解读的详细内容,更多关于logback ThresholdFilter日志过滤器的资料请关注脚本之家其它相关文章!
相关文章
详解Spring MVC如何测试Controller(使用springmvc mock测试)
这篇文章主要介绍了详解Spring MVC如何测试Controller(使用springmvc mock测试),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-12-12Spring的@PreAuthorize注解自定义权限校验详解
这篇文章主要介绍了Spring的@PreAuthorize注解自定义权限校验详解,由于项目中,需要对外开放接口,要求做请求头校验,不做其他权限控制,所以准备对开放的接口全部放行,不做登录校验,需要的朋友可以参考下2023-11-11Spring Cloud中Sentinel的两种限流模式介绍
如何使用Sentinel做流量控制呢?这篇文章就来为大家详细介绍了Spring Cloud中Sentinel的两种限流模式,感兴趣的小伙伴可以跟随小编一起学习一下2023-05-05JDK1.8中的ConcurrentHashMap使用及场景分析
这篇文章主要介绍了JDK1.8中的ConcurrentHashMap使用及场景分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-01-01
最新评论