View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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&uuml;lc&uuml; 
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          // level of event is less than minimum
72          return Filter.DENY;
73      }
74  
75      if(this.levelMax != null
76        && event.getLevel().toInt() > levelMax.toInt()) {
77          // level of event is greater than maximum
78          // Alas, there is no Level.isGreater method. and using
79          // a combo of isGreaterOrEqual && !Equal seems worse than
80          // checking the int values of the level objects..
81          return Filter.DENY;
82      }
83  
84      if (acceptOnMatch) {
85        // this filter set up to bypass later filters and always return
86        // accept if level in range
87        return Filter.ACCEPT;
88      }
89      else {
90        // event is ok for this filter; allow later filters to have a look..
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