1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.filter;
19
20 import org.apache.log4j.Level;
21 import org.apache.log4j.spi.Filter;
22 import org.apache.log4j.spi.LoggingEvent;
23
24 /***
25 This is a very simple filter based on level matching, which can be
26 used to reject messages with priorities outside a certain range.
27
28 <p>The filter admits three options <b>LevelMin</b>, <b>LevelMax</b>
29 and <b>AcceptOnMatch</b>.
30
31 <p>If the level of the {@link org.apache.log4j.spi.LoggingEvent} is not between Min and Max
32 (inclusive), then {@link org.apache.log4j.spi.Filter#DENY} is returned.
33
34 <p> If the Logging event level is within the specified range, then if
35 <b>AcceptOnMatch</b> is true, {@link org.apache.log4j.spi.Filter#ACCEPT} is returned, and if
36 <b>AcceptOnMatch</b> is false, {@link org.apache.log4j.spi.Filter#NEUTRAL} is returned.
37
38 <p>If <code>LevelMin</code> is not defined, then there is no
39 minimum acceptable level (ie a level is never rejected for
40 being too "low"/unimportant). If <code>LevelMax</code> is not
41 defined, then there is no maximum acceptable level (ie a
42 level is never rejected for beeing too "high"/important).
43
44 <p>Refer to the {@link
45 org.apache.log4j.AppenderSkeleton#setThreshold setThreshold} method
46 available to <code>all</code> appenders extending {@link
47 org.apache.log4j.AppenderSkeleton} for a more convenient way to
48 filter out events by level.
49
50 @author Simon Kitching
51 @author based on code by Ceki Gülcü
52 */
53 public class LevelRangeFilter extends Filter {
54
55 /***
56 Do we return ACCEPT when a match occurs. Default is
57 <code>false</code>, so that later filters get run by default */
58 boolean acceptOnMatch = false;
59
60 Level levelMin;
61 Level levelMax;
62
63
64 /***
65 Return the decision of this filter.
66 */
67 public
68 int decide(LoggingEvent event) {
69 if(this.levelMin != null
70 && event.getLevel().isGreaterOrEqual(levelMin) == false) {
71
72 return Filter.DENY;
73 }
74
75 if(this.levelMax != null
76 && event.getLevel().toInt() > levelMax.toInt()) {
77
78
79
80
81 return Filter.DENY;
82 }
83
84 if (acceptOnMatch) {
85
86
87 return Filter.ACCEPT;
88 }
89 else {
90
91 return Filter.NEUTRAL;
92 }
93 }
94
95 /***
96 Get the value of the <code>LevelMax</code> option. */
97 public
98 Level getLevelMax() {
99 return levelMax;
100 }
101
102
103 /***
104 Get the value of the <code>LevelMin</code> option. */
105 public
106 Level getLevelMin() {
107 return levelMin;
108 }
109
110 /***
111 Get the value of the <code>AcceptOnMatch</code> option.
112 */
113 public
114 boolean getAcceptOnMatch() {
115 return acceptOnMatch;
116 }
117
118 /***
119 Set the <code>LevelMax</code> option.
120 */
121 public
122 void setLevelMax(Level levelMax) {
123 this.levelMax = levelMax;
124 }
125
126 /***
127 Set the <code>LevelMin</code> option.
128 */
129 public
130 void setLevelMin(Level levelMin) {
131 this.levelMin = levelMin;
132 }
133
134 /***
135 Set the <code>AcceptOnMatch</code> option.
136 */
137 public
138 void setAcceptOnMatch(boolean acceptOnMatch) {
139 this.acceptOnMatch = acceptOnMatch;
140 }
141 }
142