001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.log4j.varia; 019 020import org.apache.log4j.Level; 021import org.apache.log4j.spi.Filter; 022import org.apache.log4j.spi.LoggingEvent; 023 024/** 025 This is a very simple filter based on level matching, which can be 026 used to reject messages with priorities outside a certain range. 027 028 <p>The filter admits three options <b>LevelMin</b>, <b>LevelMax</b> 029 and <b>AcceptOnMatch</b>. 030 031 <p>If the level of the {@link LoggingEvent} is not between Min and Max 032 (inclusive), then {@link Filter#DENY} is returned. 033 034 <p> If the Logging event level is within the specified range, then if 035 <b>AcceptOnMatch</b> is true, {@link Filter#ACCEPT} is returned, and if 036 <b>AcceptOnMatch</b> is false, {@link Filter#NEUTRAL} is returned. 037 038 <p>If <code>LevelMin</code>w is not defined, then there is no 039 minimum acceptable level (ie a level is never rejected for 040 being too "low"/unimportant). If <code>LevelMax</code> is not 041 defined, then there is no maximum acceptable level (ie a 042 level is never rejected for beeing too "high"/important). 043 044 <p>Refer to the {@link 045 org.apache.log4j.AppenderSkeleton#setThreshold setThreshold} method 046 available to <code>all</code> appenders extending {@link 047 org.apache.log4j.AppenderSkeleton} for a more convenient way to 048 filter out events by level. 049 050 @author Simon Kitching 051 @author based on code by Ceki Gülcü 052*/ 053public class LevelRangeFilter extends Filter { 054 055 /** 056 Do we return ACCEPT when a match occurs. Default is 057 <code>false</code>, so that later filters get run by default */ 058 boolean acceptOnMatch = false; 059 060 Level levelMin; 061 Level levelMax; 062 063 064 /** 065 Return the decision of this filter. 066 */ 067 public 068 int decide(LoggingEvent event) { 069 if(this.levelMin != null) { 070 if (event.getLevel().isGreaterOrEqual(levelMin) == false) { 071 // level of event is less than minimum 072 return Filter.DENY; 073 } 074 } 075 076 if(this.levelMax != null) { 077 if (event.getLevel().toInt() > levelMax.toInt()) { 078 // level of event is greater than maximum 079 // Alas, there is no Level.isGreater method. and using 080 // a combo of isGreaterOrEqual && !Equal seems worse than 081 // checking the int values of the level objects.. 082 return Filter.DENY; 083 } 084 } 085 086 if (acceptOnMatch) { 087 // this filter set up to bypass later filters and always return 088 // accept if level in range 089 return Filter.ACCEPT; 090 } 091 else { 092 // event is ok for this filter; allow later filters to have a look.. 093 return Filter.NEUTRAL; 094 } 095 } 096 097 /** 098 Get the value of the <code>LevelMax</code> option. */ 099 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