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.varia; 19 20 import org.apache.log4j.Level; 21 import org.apache.log4j.spi.Filter; 22 import org.apache.log4j.spi.LoggingEvent; 23 import org.apache.log4j.helpers.OptionConverter; 24 25 /** 26 This is a very simple filter based on level matching. 27 28 <p>The filter admits two options <b>LevelToMatch</b> and 29 <b>AcceptOnMatch</b>. If there is an exact match between the value 30 of the <b>LevelToMatch</b> option and the level of the {@link 31 LoggingEvent}, then the {@link #decide} method returns {@link 32 Filter#ACCEPT} in case the <b>AcceptOnMatch</b> option value is set 33 to <code>true</code>, if it is <code>false</code> then {@link 34 Filter#DENY} is returned. If there is no match, {@link 35 Filter#NEUTRAL} is returned. 36 37 @author Ceki Gülcü 38 39 @since 1.2 */ 40 public class LevelMatchFilter extends Filter { 41 42 /** 43 Do we return ACCEPT when a match occurs. Default is 44 <code>true</code>. */ 45 boolean acceptOnMatch = true; 46 47 /** 48 */ 49 Level levelToMatch; 50 51 52 public 53 void setLevelToMatch(String level) { 54 levelToMatch = OptionConverter.toLevel(level, null); 55 } 56 57 public 58 String getLevelToMatch() { 59 return levelToMatch == null ? null : levelToMatch.toString(); 60 } 61 62 public 63 void setAcceptOnMatch(boolean acceptOnMatch) { 64 this.acceptOnMatch = acceptOnMatch; 65 } 66 67 public 68 boolean getAcceptOnMatch() { 69 return acceptOnMatch; 70 } 71 72 73 /** 74 Return the decision of this filter. 75 76 Returns {@link Filter#NEUTRAL} if the <b>LevelToMatch</b> option 77 is not set or if there is not match. Otherwise, if there is a 78 match, then the returned decision is {@link Filter#ACCEPT} if the 79 <b>AcceptOnMatch</b> property is set to <code>true</code>. The 80 returned decision is {@link Filter#DENY} if the 81 <b>AcceptOnMatch</b> property is set to false. 82 83 */ 84 public 85 int decide(LoggingEvent event) { 86 if(this.levelToMatch == null) { 87 return Filter.NEUTRAL; 88 } 89 90 boolean matchOccured = false; 91 if(this.levelToMatch.equals(event.getLevel())) { 92 matchOccured = true; 93 } 94 95 if(matchOccured) { 96 if(this.acceptOnMatch) 97 return Filter.ACCEPT; 98 else 99 return Filter.DENY; 100 } else { 101 return Filter.NEUTRAL; 102 } 103 } 104 }