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.rolling;
19  
20  import org.apache.log4j.Appender;
21  import org.apache.log4j.spi.Filter;
22  import org.apache.log4j.spi.LoggingEvent;
23  import org.apache.log4j.spi.OptionHandler;
24  import org.apache.log4j.spi.TriggeringEventEvaluator;
25  import org.apache.log4j.xml.UnrecognizedElementHandler;
26  import org.w3c.dom.Element;
27  
28  import java.util.Properties;
29  
30  
31  /***
32   * FilterBasedTriggeringPolicy determines if rolling should be triggered
33   * by evaluating the current message against a set of filters.  Unless a
34   * filter rejects a message, a rolling event will be triggered.
35   *
36   * @author Curt Arnold
37   *
38   */
39  public final class FilterBasedTriggeringPolicy
40          implements TriggeringPolicy, TriggeringEventEvaluator, UnrecognizedElementHandler {
41    /***
42     * The first filter in the filter chain. Set to <code>null</code> initially.
43     */
44    private Filter headFilter;
45  
46    /***
47     * The last filter in the filter chain.
48     */
49    private Filter tailFilter;
50  
51    /***
52     *  Creates a new FilterBasedTriggeringPolicy.
53     */
54    public FilterBasedTriggeringPolicy() {
55    }
56  
57      /***
58       * {@inheritDoc}
59       */
60    public boolean isTriggeringEvent(LoggingEvent event) {
61      //
62      //   in the abnormal case of no contained filters
63      //     always return true to avoid each logging event
64      //     from having its own file.
65      if (headFilter == null) {
66        return false;
67      }
68  
69      //
70      //    otherwise loop through the filters
71      //
72      for (Filter f = headFilter; f != null; f = f.next) {
73        switch (f.decide(event)) {
74        case Filter.DENY:
75          return false;
76  
77        case Filter.ACCEPT:
78          return true;
79        }
80      }
81  
82      return true;
83     }
84  
85  
86    /***
87     * {@inheritDoc}
88     *
89     */
90    public boolean isTriggeringEvent(
91      final Appender appender, final LoggingEvent event, final String file,
92      final long fileLength) {
93      return isTriggeringEvent(event);
94    }
95  
96    /***
97     * Add a filter to end of the filter list.
98     * @param newFilter filter to add to end of list.
99     */
100   public void addFilter(final Filter newFilter) {
101     if (headFilter == null) {
102       headFilter = newFilter;
103       tailFilter = newFilter;
104     } else {
105       tailFilter.next = newFilter;
106       tailFilter = newFilter;
107     }
108   }
109 
110   /***
111    * Clear the filters chain.
112    *
113    */
114   public void clearFilters() {
115     headFilter = null;
116     tailFilter = null;
117   }
118 
119   /***
120    * Returns the head Filter.
121    * @return head of filter chain, may be null.
122    *
123    */
124   public Filter getFilter() {
125     return headFilter;
126   }
127 
128   /***
129    *  {@inheritDoc}
130    */
131   public void activateOptions() {
132     for (Filter f = headFilter; f != null; f = f.next) {
133       f.activateOptions();
134     }
135   }
136 
137     /***
138      * {@inheritDoc}
139      */
140   public boolean parseUnrecognizedElement(final Element element,
141                                           final Properties props) throws Exception {
142       final String nodeName = element.getNodeName();
143       if ("filter".equals(nodeName)) {
144           OptionHandler filter =
145                   org.apache.log4j.extras.DOMConfigurator.parseElement(
146                           element, props, Filter.class);
147           if (filter instanceof Filter) {
148               filter.activateOptions();
149               this.addFilter((Filter) filter);
150           }
151           return true;
152       }
153       return false;
154   }
155 
156 }