本文主要研究一下logback的LevelFilter
ch/qos/logback/core/filter/AbstractMatcherFilter.java
public abstract class AbstractMatcherFilter extends Filter {
protected FilterReply onMatch = FilterReply.NEUTRAL;
protected FilterReply onMismatch = FilterReply.NEUTRAL;
final public void setOnMatch(FilterReply reply) {
this.onMatch = reply;
}
final public void setOnMismatch(FilterReply reply) {
this.onMismatch = reply;
}
final public FilterReply getOnMatch() {
return onMatch;
}
final public FilterReply getOnMismatch() {
return onMismatch;
}
}
AbstractMatcherFilter继承了Filter,它定义了onMatch及onMismatch属性
ch/qos/logback/classic/filter/LevelFilter.java
public class LevelFilter extends AbstractMatcherFilter {
Level level;
@Override
public FilterReply decide(ILoggingEvent event) {
if (!isStarted()) {
return FilterReply.NEUTRAL;
}
if (event.getLevel().equals(level)) {
return onMatch;
} else {
return onMismatch;
}
}
public void setLevel(Level level) {
this.level = level;
}
public void start() {
if (this.level != null) {
super.start();
}
}
}
LevelFilter继承了AbstractMatcherFilter,其decide判断event的level等级是否与配置的level一致,一致则返回onMatch,否则返回onMismatch
ERROR
ACCEPT
DENY
${CONSOLE_LOG_PATTERN}
这里CONSOLE的appender定义了LevelFilter,当level为ERROR级别时ACCEPT,否则DENY
logback提供了LevelFilter,可以配置指定的level、onMatch、onMismatch属性,用于设置指定appender的打印级别。