View Javadoc

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