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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.log4j.varia;
20 import org.apache.log4j.Level;
21 import org.apache.log4j.spi.Filter;
22 import org.apache.log4j.spi.LoggingEvent;
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.
28 <p>The filter admits three options <b>LevelMin</b>, <b>LevelMax</b>
29 and <b>AcceptOnMatch</b>.
31 <p>If the level of the {@link LoggingEvent} is not between Min and Max
32 (inclusive), then {@link Filter#DENY} is returned.
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.
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).
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.
50 @author Simon Kitching
51 @author based on code by Ceki Gülcü
53 public class LevelRangeFilter extends Filter {
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;
65 Return the decision of this filter.
68 int decide(LoggingEvent event) {
69 if(this.levelMin != null) {
70 if (event.getLevel().isGreaterOrEqual(levelMin) == false) {
71 // level of event is less than minimum
76 if(this.levelMax != null) {
77 if (event.getLevel().toInt() > levelMax.toInt()) {
78 // level of event is greater than maximum
79 // Alas, there is no Level.isGreater method. and using
80 // a combo of isGreaterOrEqual && !Equal seems worse than
81 // checking the int values of the level objects..
87 // this filter set up to bypass later filters and always return
88 // accept if level in range
92 // event is ok for this filter; allow later filters to have a look..
93 return Filter.NEUTRAL;
98 Get the value of the <code>LevelMax</code> option. */
100 Level getLevelMax() {
106 Get the value of the <code>LevelMin</code> option. */
108 Level getLevelMin() {
113 Get the value of the <code>AcceptOnMatch</code> option.
116 boolean getAcceptOnMatch() {
117 return acceptOnMatch;
121 Set the <code>LevelMax</code> option.
124 void setLevelMax(Level levelMax) {
125 this.levelMax = levelMax;
129 Set the <code>LevelMin</code> option.
132 void setLevelMin(Level levelMin) {
133 this.levelMin = levelMin;
137 Set the <code>AcceptOnMatch</code> option.
140 void setAcceptOnMatch(boolean acceptOnMatch) {
141 this.acceptOnMatch = acceptOnMatch;