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.spi.Filter;
21  import org.apache.log4j.spi.LoggingEvent;
22  
23  
24  /***
25     This is a very simple filter based on logger name matching.
26  
27     <p>The filter admits two options <b>LoggerToMatch</b> and
28     <b>AcceptOnMatch</b>. If there is an exact match between the value
29     of the <b>LoggerToMatch</b> option and the logger of the {@link
30     org.apache.log4j.spi.LoggingEvent}, then the {@link #decide} method returns {@link
31     org.apache.log4j.spi.Filter#ACCEPT} in case the <b>AcceptOnMatch</b> option value is set
32     to <code>true</code>, if it is <code>false</code> then {@link
33     org.apache.log4j.spi.Filter#DENY} is returned. If there is no match, {@link
34     org.apache.log4j.spi.Filter#NEUTRAL} is returned.  A loggerToMatch of "root"
35     matches both the root logger and a logger named "root".
36  
37     */
38  public class LoggerMatchFilter extends Filter {
39    /***
40       Do we return ACCEPT when a match occurs. Default is
41       <code>true</code>.  */
42    private boolean acceptOnMatch = true;
43  
44    /***
45     * Logger name, may be null or empty in which case it matches root.
46     */
47    private String loggerToMatch = "root";
48  
49      /***
50       * Sets logger name.
51       * @param logger logger name.
52       */
53    public void setLoggerToMatch(final String logger) {
54      if (logger == null) {
55          loggerToMatch = "root";
56      } else {
57          loggerToMatch = logger;
58      }
59    }
60  
61      /***
62       * Gets logger name.
63       * @return logger name.
64       */
65    public String getLoggerToMatch() {
66      return loggerToMatch;
67    }
68  
69      /***
70       * Sets whether a match should result in acceptance.
71       * @param acceptOnMatch if true, accept if logger name matches, otherwise reject.
72       */
73    public void setAcceptOnMatch(final boolean acceptOnMatch) {
74      this.acceptOnMatch = acceptOnMatch;
75    }
76  
77      /***
78       * Gets whether a match should result in acceptance.
79       * @return true if event is accepted if logger name matches.
80       */
81    public boolean getAcceptOnMatch() {
82      return acceptOnMatch;
83    }
84  
85  
86    /***
87     * {@inheritDoc}
88    */
89    public int decide(final LoggingEvent event) {
90      boolean matchOccured = loggerToMatch.equals(event.getLoggerName());
91      if (matchOccured) {
92        if (this.acceptOnMatch) {
93          return Filter.ACCEPT;
94        } else {
95          return Filter.DENY;
96        }
97      } else {
98        return Filter.NEUTRAL;
99      }
100   }
101 }