1 public class LevelMatchFilter extends Filter {
2 boolean acceptOnMatch = true;
3 Level levelToMatch;
4 public int decide(LoggingEvent event) {
5 if (this.levelToMatch == null) {
6 return Filter.NEUTRAL;
7 }
8 boolean matchOccured = false;
9 if (this.levelToMatch.equals(event.getLevel())) {
10 matchOccured = true;
11 }
12 if (matchOccured) {
13 if (this.acceptOnMatch)
14 return Filter.ACCEPT;
15 else
16 return Filter.DENY;
17 } else {
18 return Filter.NEUTRAL;
19 }
20 }
21 }
LevelRangeFilter判断日志级别是否在设置的级别范围内以决定Filter后的状态:
1 public class LevelRangeFilter extends Filter {
3 Level levelMin;
4 Level levelMax;
5 public int decide(LoggingEvent event) {
6 if (this.levelMin != null) {
7 if (event.getLevel().isGreaterOrEqual(levelMin) == false) {
8 return Filter.DENY;
9 }
10 }
11 if (this.levelMax != null) {
12 if (event.getLevel().toInt() > levelMax.toInt()) {
13 return Filter.DENY;
14 }
15 }
16 if (acceptOnMatch) {
17 return Filter.ACCEPT;
18 } else {
19 return Filter.NEUTRAL;
20 }
21 }
22 }
作者:上善若水