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;
23 import org.apache.log4j.helpers.OptionConverter;
26 This is a very simple filter based on level matching.
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.
37 @author Ceki Gülcü
40 public class LevelMatchFilter extends Filter {
43 Do we return ACCEPT when a match occurs. Default is
45 boolean acceptOnMatch = true;
53 void setLevelToMatch(String level) {
54 levelToMatch = OptionConverter.toLevel(level, null);
58 String getLevelToMatch() {
59 return levelToMatch == null ? null : levelToMatch.toString();
63 void setAcceptOnMatch(boolean acceptOnMatch) {
64 this.acceptOnMatch = acceptOnMatch;
68 boolean getAcceptOnMatch() {
74 Return the decision of this filter.
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.
85 int decide(LoggingEvent event) {
86 if(this.levelToMatch == null) {
87 return Filter.NEUTRAL;
90 boolean matchOccured = false;
91 if(this.levelToMatch.equals(event.getLevel())) {
96 if(this.acceptOnMatch) {
102 return Filter.NEUTRAL;