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.spi;
19  
20  
21  
22  /**
23     Users should extend this class to implement customized logging
24     event filtering. Note that {@link org.apache.log4j.Category} and {@link
25     org.apache.log4j.AppenderSkeleton}, the parent class of all standard
26     appenders, have built-in filtering rules. It is suggested that you
27     first use and understand the built-in rules before rushing to write
28     your own custom filters.
29  
30     <p>This abstract class assumes and also imposes that filters be
31     organized in a linear chain. The {@link #decide
32     decide(LoggingEvent)} method of each filter is called sequentially,
33     in the order of their addition to the chain.
34  
35     <p>The {@link #decide decide(LoggingEvent)} method must return one
36     of the integer constants {@link #DENY}, {@link #NEUTRAL} or {@link
37     #ACCEPT}.
38  
39     <p>If the value {@link #DENY} is returned, then the log event is
40     dropped immediately without consulting with the remaining
41     filters. 
42  
43     <p>If the value {@link #NEUTRAL} is returned, then the next filter
44     in the chain is consulted. If there are no more filters in the
45     chain, then the log event is logged. Thus, in the presence of no
46     filters, the default behaviour is to log all logging events.
47  
48     <p>If the value {@link #ACCEPT} is returned, then the log
49     event is logged without consulting the remaining filters. 
50  
51     <p>The philosophy of log4j filters is largely inspired from the
52     Linux ipchains. 
53  
54     <p>Note that filtering is only supported by the {@link
55     org.apache.log4j.xml.DOMConfigurator DOMConfigurator}. The {@link
56     org.apache.log4j.PropertyConfigurator PropertyConfigurator} does not
57     support filters.
58  
59     @author Ceki G&uuml;lc&uuml;
60     @since 0.9.0 */
61  public abstract class Filter implements OptionHandler {
62  
63    /**
64       Points to the next filter in the filter chain.
65  
66       @deprecated As of 1.2.12, use {@link #getNext} and {@link #setNext} instead
67     */
68    public Filter next;
69  
70    /**
71       The log event must be dropped immediately without consulting
72       with the remaining filters, if any, in the chain.  */
73    public static final int DENY    = -1;
74    
75    /**
76       This filter is neutral with respect to the log event. The
77       remaining filters, if any, should be consulted for a final decision.
78    */
79    public static final int NEUTRAL = 0;
80  
81    /**
82       The log event must be logged immediately without consulting with
83       the remaining filters, if any, in the chain.  */
84    public static final int ACCEPT  = 1;
85  
86  
87    /**
88       Usually filters options become active when set. We provide a
89       default do-nothing implementation for convenience.
90    */
91    public
92    void activateOptions() {
93    }
94  
95  
96  
97    /**     
98       <p>If the decision is <code>DENY</code>, then the event will be
99       dropped. If the decision is <code>NEUTRAL</code>, then the next
100      filter, if any, will be invoked. If the decision is ACCEPT then
101      the event will be logged without consulting with other filters in
102      the chain.
103 
104      @param event The LoggingEvent to decide upon.
105      @return decision The decision of the filter.  */
106   abstract
107   public
108   int decide(LoggingEvent event);
109 
110   /**
111    * Set the next filter pointer.
112    */ 
113   public void setNext(Filter next) {
114     this.next = next;
115   }
116  
117   /**
118    * Return the pointer to the next filter;
119    */ 
120   public Filter getNext() {
121         return next;
122   }
123 
124 }