java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > logback ThresholdFilter日志过滤器

logback ThresholdFilter临界值日志过滤器源码解读

作者:codecraft

这篇文章主要为大家介绍了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日志过滤器的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文