本文主要研究一下logback的ThresholdFilter
ch/qos/logback/classic/filter/ThresholdFilter.java
public class ThresholdFilter extends Filter {
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
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 OFF
is used to turn off logging.
*/
public static final Level OFF = new Level(OFF_INT, "OFF");
/**
* The ERROR
level designates error events which may or not be
* fatal to the application.
*/
public static final Level ERROR = new Level(ERROR_INT, "ERROR");
/**
* The WARN
level designates potentially harmful situations.
*/
public static final Level WARN = new Level(WARN_INT, "WARN");
/**
* The INFO
level designates informational messages highlighting
* overall progress of the application.
*/
public static final Level INFO = new Level(INFO_INT, "INFO");
/**
* The DEBUG
level designates informational events of lower
* importance.
*/
public static final Level DEBUG = new Level(DEBUG_INT, "DEBUG");
/**
* The TRACE
level designates informational events of very low
* importance.
*/
public static final Level TRACE = new Level(TRACE_INT, "TRACE");
/**
* The ALL
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 true
if this Level has a higher or equal Level than the
* Level passed as argument, false
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值来判断。
infoLog.log
infoLogs.%d{yyyy-MM-dd}.log
30
3GB
INFO
%-4relative [%thread] %-5level %logger{35} - %msg%n
这里对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值来判断。