View Javadoc

1   /*
2    * Copyright 1999,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.log4j.rolling;
18  
19  import org.apache.log4j.Appender;
20  import org.apache.log4j.spi.Filter;
21  import org.apache.log4j.spi.LoggingEvent;
22  import org.apache.log4j.spi.OptionHandler;
23  import org.apache.log4j.spi.TriggeringEventEvaluator;
24  import org.apache.log4j.xml.UnrecognizedElementHandler;
25  import org.w3c.dom.Element;
26  
27  import java.util.Properties;
28  
29  
30  /***
31   * FilterBasedTriggeringPolicy determines if rolling should be triggered
32   * by evaluating the current message against a set of filters.  Unless a
33   * filter rejects a message, a rolling event will be triggered.
34   *
35   * @author Curt Arnold
36   *
37   */
38  public final class FilterBasedTriggeringPolicy
39          implements TriggeringPolicy, TriggeringEventEvaluator, UnrecognizedElementHandler {
40    /***
41     * The first filter in the filter chain. Set to <code>null</code> initially.
42     */
43    private Filter headFilter;
44  
45    /***
46     * The last filter in the filter chain.
47     */
48    private Filter tailFilter;
49  
50    /***
51     *  Creates a new FilterBasedTriggeringPolicy.
52     */
53    public FilterBasedTriggeringPolicy() {
54    }
55  
56      /***
57       * {@inheritDoc}
58       */
59    public boolean isTriggeringEvent(LoggingEvent event) {
60      //
61      //   in the abnormal case of no contained filters
62      //     always return true to avoid each logging event
63      //     from having its own file.
64      if (headFilter == null) {
65        return false;
66      }
67  
68      //
69      //    otherwise loop through the filters
70      //
71      for (Filter f = headFilter; f != null; f = f.next) {
72        switch (f.decide(event)) {
73        case Filter.DENY:
74          return false;
75  
76        case Filter.ACCEPT:
77          return true;
78        }
79      }
80  
81      return true;
82     }
83  
84  
85    /***
86     * {@inheritDoc}
87     *
88     */
89    public boolean isTriggeringEvent(
90      final Appender appender, final LoggingEvent event, final String file,
91      final long fileLength) {
92      return isTriggeringEvent(event);
93    }
94  
95    /***
96     * Add a filter to end of the filter list.
97     * @param newFilter filter to add to end of list.
98     */
99    public void addFilter(final Filter newFilter) {
100     if (headFilter == null) {
101       headFilter = newFilter;
102       tailFilter = newFilter;
103     } else {
104       tailFilter.next = newFilter;
105       tailFilter = newFilter;
106     }
107   }
108 
109   /***
110    * Clear the filters chain.
111    *
112    */
113   public void clearFilters() {
114     headFilter = null;
115     tailFilter = null;
116   }
117 
118   /***
119    * Returns the head Filter.
120    * @return head of filter chain, may be null.
121    *
122    */
123   public Filter getFilter() {
124     return headFilter;
125   }
126 
127   /***
128    *  {@inheritDoc}
129    */
130   public void activateOptions() {
131     for (Filter f = headFilter; f != null; f = f.next) {
132       f.activateOptions();
133     }
134   }
135 
136     /***
137      * {@inheritDoc}
138      */
139   public boolean parseUnrecognizedElement(final Element element,
140                                           final Properties props) throws Exception {
141       final String nodeName = element.getNodeName();
142       if ("filter".equals(nodeName)) {
143           OptionHandler filter =
144                   org.apache.log4j.extras.DOMConfigurator.parseElement(
145                           element, props, Filter.class);
146           if (filter instanceof Filter) {
147               filter.activateOptions();
148               this.addFilter((Filter) filter);
149           }
150           return true;
151       }
152       return false;
153   }
154 
155 }