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.spi.Filter;
21 import org.apache.log4j.spi.LoggingEvent;
22 import org.apache.log4j.helpers.OptionConverter;
25 * This is a very simple filter based on string matching.
27 * <p>The filter admits two options <b>StringToMatch</b> and
28 * <b>AcceptOnMatch</b>. If there is a match between the value of the
29 * StringToMatch option and the message of the {@link org.apache.log4j.spi.LoggingEvent},
30 * then the {@link #decide(LoggingEvent)} method returns {@link org.apache.log4j.spi.Filter#ACCEPT} if
31 * the <b>AcceptOnMatch</b> option value is true, if it is false then
32 * {@link org.apache.log4j.spi.Filter#DENY} is returned. If there is no match, {@link
33 * org.apache.log4j.spi.Filter#NEUTRAL} is returned.
35 * @author Ceki Gülcü
38 public class StringMatchFilter extends Filter {
41 @deprecated Options are now handled using the JavaBeans paradigm.
42 This constant is not longer needed and will be removed in the
45 public static final String STRING_TO_MATCH_OPTION = "StringToMatch";
48 @deprecated Options are now handled using the JavaBeans paradigm.
49 This constant is not longer needed and will be removed in the
52 public static final String ACCEPT_ON_MATCH_OPTION = "AcceptOnMatch";
54 boolean acceptOnMatch = true;
58 @deprecated We now use JavaBeans introspection to configure
59 components. Options strings are no longer needed.
62 String[] getOptionStrings() {
63 return new String[] {STRING_TO_MATCH_OPTION, ACCEPT_ON_MATCH_OPTION};
67 @deprecated Use the setter method for the option directly instead
68 of the generic <code>setOption</code> method.
71 void setOption(String key, String value) {
73 if(key.equalsIgnoreCase(STRING_TO_MATCH_OPTION)) {
74 stringToMatch = value;
75 } else if (key.equalsIgnoreCase(ACCEPT_ON_MATCH_OPTION)) {
76 acceptOnMatch = OptionConverter.toBoolean(value, acceptOnMatch);
81 void setStringToMatch(String s) {
86 String getStringToMatch() {
91 void setAcceptOnMatch(boolean acceptOnMatch) {
92 this.acceptOnMatch = acceptOnMatch;
96 boolean getAcceptOnMatch() {
101 Returns {@link Filter#NEUTRAL} is there is no string match.
104 int decide(LoggingEvent event) {
105 String msg = event.getRenderedMessage();
107 if(msg == null || stringToMatch == null) {
108 return Filter.NEUTRAL;
112 if( msg.indexOf(stringToMatch) == -1 ) {
113 return Filter.NEUTRAL;
114 } else { // we've got a match
116 return Filter.ACCEPT;