1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.filter;
18
19 import org.apache.logging.log4j.Level;
20 import org.apache.logging.log4j.Marker;
21 import org.apache.logging.log4j.core.Filter;
22 import org.apache.logging.log4j.core.LogEvent;
23 import org.apache.logging.log4j.core.Logger;
24 import org.apache.logging.log4j.core.config.Node;
25 import org.apache.logging.log4j.core.config.plugins.Plugin;
26 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
27 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
28 import org.apache.logging.log4j.message.Message;
29
30
31
32
33
34
35
36
37
38
39
40 @Plugin(name = "LevelRangeFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
41 public final class LevelRangeFilter extends AbstractFilter {
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 @PluginFactory
57 public static LevelRangeFilter createFilter(
58
59 @PluginAttribute("minLevel") final Level minLevel,
60 @PluginAttribute("maxLevel") final Level maxLevel,
61 @PluginAttribute("onMatch") final Result match,
62 @PluginAttribute("onMismatch") final Result mismatch) {
63
64 final Level actualMinLevel = minLevel == null ? Level.ERROR : minLevel;
65 final Level actualMaxLevel = maxLevel == null ? Level.ERROR : maxLevel;
66 final Result onMatch = match == null ? Result.NEUTRAL : match;
67 final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
68 return new LevelRangeFilter(actualMinLevel, actualMaxLevel, onMatch, onMismatch);
69 }
70 private final Level maxLevel;
71
72 private final Level minLevel;
73
74 private LevelRangeFilter(final Level minLevel, final Level maxLevel, final Result onMatch, final Result onMismatch) {
75 super(onMatch, onMismatch);
76 this.minLevel = minLevel;
77 this.maxLevel = maxLevel;
78 }
79
80 private Result filter(final Level level) {
81 return level.isInRange(this.minLevel, this.maxLevel) ? onMatch : onMismatch;
82 }
83
84 @Override
85 public Result filter(final LogEvent event) {
86 return filter(event.getLevel());
87 }
88
89 @Override
90 public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
91 final Throwable t) {
92 return filter(level);
93 }
94
95 @Override
96 public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
97 final Throwable t) {
98 return filter(level);
99 }
100
101 @Override
102 public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
103 final Object... params) {
104 return filter(level);
105 }
106
107 public Level getMinLevel() {
108 return minLevel;
109 }
110
111 @Override
112 public String toString() {
113 return minLevel.toString();
114 }
115
116 }