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; 023import org.apache.log4j.helpers.OptionConverter; 024 025/** 026 This is a very simple filter based on level matching. 027 028 <p>The filter admits two options <b>LevelToMatch</b> and 029 <b>AcceptOnMatch</b>. If there is an exact match between the value 030 of the <b>LevelToMatch</b> option and the level of the {@link 031 LoggingEvent}, then the {@link #decide} method returns {@link 032 Filter#ACCEPT} in case the <b>AcceptOnMatch</b> option value is set 033 to <code>true</code>, if it is <code>false</code> then {@link 034 Filter#DENY} is returned. If there is no match, {@link 035 Filter#NEUTRAL} is returned. 036 037 @author Ceki Gülcü 038 039 @since 1.2 */ 040public class LevelMatchFilter extends Filter { 041 042 /** 043 Do we return ACCEPT when a match occurs. Default is 044 <code>true</code>. */ 045 boolean acceptOnMatch = true; 046 047 /** 048 */ 049 Level levelToMatch; 050 051 052 public 053 void setLevelToMatch(String level) { 054 levelToMatch = OptionConverter.toLevel(level, null); 055 } 056 057 public 058 String getLevelToMatch() { 059 return levelToMatch == null ? null : levelToMatch.toString(); 060 } 061 062 public 063 void setAcceptOnMatch(boolean acceptOnMatch) { 064 this.acceptOnMatch = acceptOnMatch; 065 } 066 067 public 068 boolean getAcceptOnMatch() { 069 return acceptOnMatch; 070 } 071 072 073 /** 074 Return the decision of this filter. 075 076 Returns {@link Filter#NEUTRAL} if the <b>LevelToMatch</b> option 077 is not set or if there is not match. Otherwise, if there is a 078 match, then the returned decision is {@link Filter#ACCEPT} if the 079 <b>AcceptOnMatch</b> property is set to <code>true</code>. The 080 returned decision is {@link Filter#DENY} if the 081 <b>AcceptOnMatch</b> property is set to false. 082 083 */ 084 public 085 int decide(LoggingEvent event) { 086 if(this.levelToMatch == null) { 087 return Filter.NEUTRAL; 088 } 089 090 boolean matchOccured = false; 091 if(this.levelToMatch.equals(event.getLevel())) { 092 matchOccured = true; 093 } 094 095 if(matchOccured) { 096 if(this.acceptOnMatch) 097 return Filter.ACCEPT; 098 else 099 return Filter.DENY; 100 } else { 101 return Filter.NEUTRAL; 102 } 103 } 104}