KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > varia > LevelRangeFilter


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.varia;
18
19 import org.apache.log4j.Level;
20 import org.apache.log4j.spi.Filter;
21 import org.apache.log4j.spi.LoggingEvent;
22 import org.apache.log4j.helpers.LogLog;
23 import org.apache.log4j.helpers.OptionConverter;
24
25 /**
26    This is a very simple filter based on level matching, which can be
27    used to reject messages with priorities outside a certain range.
28    
29    <p>The filter admits three options <b>LevelMin</b>, <b>LevelMax</b>
30    and <b>AcceptOnMatch</b>.
31
32    <p>If the level of the {@link LoggingEvent} is not between Min and Max
33    (inclusive), then {@link Filter#DENY} is returned.
34    
35    <p> If the Logging event level is within the specified range, then if
36    <b>AcceptOnMatch</b> is true, {@link Filter#ACCEPT} is returned, and if
37    <b>AcceptOnMatch</b> is false, {@link Filter#NEUTRAL} is returned.
38    
39    <p>If <code>LevelMin</code>w is not defined, then there is no
40    minimum acceptable level (ie a level is never rejected for
41    being too "low"/unimportant). If <code>LevelMax</code> is not
42    defined, then there is no maximum acceptable level (ie a
43    level is never rejected for beeing too "high"/important).
44
45    <p>Refer to the {@link
46    org.apache.log4j.AppenderSkeleton#setThreshold setThreshold} method
47    available to <code>all</code> appenders extending {@link
48    org.apache.log4j.AppenderSkeleton} for a more convenient way to
49    filter out events by level.
50
51    @author Simon Kitching
52    @author based on code by Ceki G&uuml;lc&uuml;
53 */

54 public class LevelRangeFilter extends Filter {
55
56   /**
57      Do we return ACCEPT when a match occurs. Default is
58      <code>false</code>, so that later filters get run by default */

59   boolean acceptOnMatch = false;
60
61   Level levelMin;
62   Level levelMax;
63
64  
65   /**
66      Return the decision of this filter.
67    */

68   public
69   int decide(LoggingEvent event) {
70     if(this.levelMin != null) {
71       if (event.getLevel().isGreaterOrEqual(levelMin) == false) {
72         // level of event is less than minimum
73
return Filter.DENY;
74       }
75     }
76
77     if(this.levelMax != null) {
78       if (event.getLevel().toInt() > levelMax.toInt()) {
79         // level of event is greater than maximum
80
// Alas, there is no Level.isGreater method. and using
81
// a combo of isGreaterOrEqual && !Equal seems worse than
82
// checking the int values of the level objects..
83
return Filter.DENY;
84       }
85     }
86
87     if (acceptOnMatch) {
88       // this filter set up to bypass later filters and always return
89
// accept if level in range
90
return Filter.ACCEPT;
91     }
92     else {
93       // event is ok for this filter; allow later filters to have a look..
94
return Filter.NEUTRAL;
95     }
96   }
97
98  /**
99      Get the value of the <code>LevelMax</code> option. */

100   public
101   Level getLevelMax() {
102     return levelMax;
103   }
104
105
106   /**
107      Get the value of the <code>LevelMin</code> option. */

108   public
109   Level getLevelMin() {
110     return levelMin;
111   }
112
113   /**
114      Get the value of the <code>AcceptOnMatch</code> option.
115    */

116   public
117   boolean getAcceptOnMatch() {
118     return acceptOnMatch;
119   }
120
121   /**
122      Set the <code>LevelMax</code> option.
123    */

124   public
125   void setLevelMax(Level levelMax) {
126     this.levelMax = levelMax;
127   }
128
129   /**
130      Set the <code>LevelMin</code> option.
131    */

132   public
133   void setLevelMin(Level levelMin) {
134     this.levelMin = levelMin;
135   }
136
137   /**
138      Set the <code>AcceptOnMatch</code> option.
139    */

140   public
141   void setAcceptOnMatch(boolean acceptOnMatch) {
142     this.acceptOnMatch = acceptOnMatch;
143   }
144 }
145
146
Popular Tags