1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.varia;
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 LoggingEvent} is not between Min and Max
32 (inclusive), then {@link 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 Filter#ACCEPT} is returned, and if
36 <b>AcceptOnMatch</b> is false, {@link Filter#NEUTRAL} is returned.
37
38 <p>If <code>LevelMin</code>w 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 if (event.getLevel().isGreaterOrEqual(levelMin) == false) {
71
72 return Filter.DENY;
73 }
74 }
75
76 if(this.levelMax != null) {
77 if (event.getLevel().toInt() > levelMax.toInt()) {
78
79
80
81
82 return Filter.DENY;
83 }
84 }
85
86 if (acceptOnMatch) {
87
88
89 return Filter.ACCEPT;
90 }
91 else {
92
93 return Filter.NEUTRAL;
94 }
95 }
96
97 /***
98 Get the value of the <code>LevelMax</code> option. */
99 public
100 Level getLevelMax() {
101 return levelMax;
102 }
103
104
105 /***
106 Get the value of the <code>LevelMin</code> option. */
107 public
108 Level getLevelMin() {
109 return levelMin;
110 }
111
112 /***
113 Get the value of the <code>AcceptOnMatch</code> option.
114 */
115 public
116 boolean getAcceptOnMatch() {
117 return acceptOnMatch;
118 }
119
120 /***
121 Set the <code>LevelMax</code> option.
122 */
123 public
124 void setLevelMax(Level levelMax) {
125 this.levelMax = levelMax;
126 }
127
128 /***
129 Set the <code>LevelMin</code> option.
130 */
131 public
132 void setLevelMin(Level levelMin) {
133 this.levelMin = levelMin;
134 }
135
136 /***
137 Set the <code>AcceptOnMatch</code> option.
138 */
139 public
140 void setAcceptOnMatch(boolean acceptOnMatch) {
141 this.acceptOnMatch = acceptOnMatch;
142 }
143 }
144