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  package org.apache.logging.log4j.core.filter;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.apache.logging.log4j.Level;
26  import org.apache.logging.log4j.Marker;
27  import org.apache.logging.log4j.core.AbstractLifeCycle;
28  import org.apache.logging.log4j.core.Filter;
29  import org.apache.logging.log4j.core.LogEvent;
30  import org.apache.logging.log4j.core.Logger;
31  import org.apache.logging.log4j.core.config.plugins.Plugin;
32  import org.apache.logging.log4j.core.config.plugins.PluginElement;
33  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
34  import org.apache.logging.log4j.message.Message;
35  
36  /**
37   * Composes and invokes one or more filters.
38   */
39  @Plugin(name = "filters", category = "Core", printObject = true)
40  public final class CompositeFilter extends AbstractLifeCycle implements Iterable<Filter>, Filter {
41  
42      private final List<Filter> filters;
43  
44      private CompositeFilter() {
45          this.filters = new ArrayList<Filter>();
46      }
47  
48      private CompositeFilter(final List<Filter> filters) {
49          if (filters == null) {
50              this.filters = Collections.unmodifiableList(new ArrayList<Filter>());
51              return;
52          }
53          this.filters = Collections.unmodifiableList(filters);
54      }
55  
56      public CompositeFilter addFilter(final Filter filter) {
57          if (filter == null) {
58              // null does nothing
59              return this;
60          }
61          final List<Filter> filterList = new ArrayList<Filter>(this.filters);
62          filterList.add(filter);
63          return new CompositeFilter(Collections.unmodifiableList(filterList));
64      }
65  
66      public CompositeFilter removeFilter(final Filter filter) {
67          if (filter == null) {
68              // null does nothing
69              return this;
70          }
71          final List<Filter> filterList = new ArrayList<Filter>(this.filters);
72          filterList.remove(filter);
73          return new CompositeFilter(Collections.unmodifiableList(filterList));
74      }
75  
76      @Override
77      public Iterator<Filter> iterator() {
78          return filters.iterator();
79      }
80  
81      public List<Filter> getFilters() {
82          return filters;
83      }
84  
85      /**
86       * Returns whether this composite contains any filters.
87       * 
88       * @return whether this composite contains any filters.
89       */
90      public boolean isEmpty() {
91          return this.filters.isEmpty();
92      }
93  
94      public int size() {
95          return filters.size();
96      }
97  
98      @Override
99      public void start() {
100         this.setStarting();
101         for (final Filter filter : filters) {
102             filter.start();
103         }
104         this.setStarted();
105     }
106 
107     @Override
108     public void stop() {
109         this.setStopping();
110         for (final Filter filter : filters) {
111             filter.stop();
112         }
113         this.setStopped();
114     }
115 
116     /**
117      * Returns the result that should be returned when the filter does not match the event.
118      *
119      * @return the Result that should be returned when the filter does not match the event.
120      */
121     @Override
122     public Result getOnMismatch() {
123         return Result.NEUTRAL;
124     }
125 
126     /**
127      * Returns the result that should be returned when the filter matches the event.
128      *
129      * @return the Result that should be returned when the filter matches the event.
130      */
131     @Override
132     public Result getOnMatch() {
133         return Result.NEUTRAL;
134     }
135 
136     /**
137      * Filter an event.
138      *
139      * @param logger
140      *            The Logger.
141      * @param level
142      *            The event logging Level.
143      * @param marker
144      *            The Marker for the event or null.
145      * @param msg
146      *            String text to filter on.
147      * @param params
148      *            An array of parameters or null.
149      * @return the Result.
150      */
151     @Override
152     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
153                          final Object... params) {
154         Result result = Result.NEUTRAL;
155         for (final Filter filter : filters) {
156             result = filter.filter(logger, level, marker, msg, params);
157             if (result == Result.ACCEPT || result == Result.DENY) {
158                 return result;
159             }
160         }
161         return result;
162     }
163 
164     /**
165      * Filter an event.
166      *
167      * @param logger
168      *            The Logger.
169      * @param level
170      *            The event logging Level.
171      * @param marker
172      *            The Marker for the event or null.
173      * @param msg
174      *            Any Object.
175      * @param t
176      *            A Throwable or null.
177      * @return the Result.
178      */
179     @Override
180     public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
181                          final Throwable t) {
182         Result result = Result.NEUTRAL;
183         for (final Filter filter : filters) {
184             result = filter.filter(logger, level, marker, msg, t);
185             if (result == Result.ACCEPT || result == Result.DENY) {
186                 return result;
187             }
188         }
189         return result;
190     }
191 
192     /**
193      * Filter an event.
194      *
195      * @param logger
196      *            The Logger.
197      * @param level
198      *            The event logging Level.
199      * @param marker
200      *            The Marker for the event or null.
201      * @param msg
202      *            The Message
203      * @param t
204      *            A Throwable or null.
205      * @return the Result.
206      */
207     @Override
208     public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
209                          final Throwable t) {
210         Result result = Result.NEUTRAL;
211         for (final Filter filter : filters) {
212             result = filter.filter(logger, level, marker, msg, t);
213             if (result == Result.ACCEPT || result == Result.DENY) {
214                 return result;
215             }
216         }
217         return result;
218     }
219 
220     /**
221      * Filter an event.
222      *
223      * @param event
224      *            The Event to filter on.
225      * @return the Result.
226      */
227     @Override
228     public Result filter(final LogEvent event) {
229         Result result = Result.NEUTRAL;
230         for (final Filter filter : filters) {
231             result = filter.filter(event);
232             if (result == Result.ACCEPT || result == Result.DENY) {
233                 return result;
234             }
235         }
236         return result;
237     }
238 
239     @Override
240     public String toString() {
241         final StringBuilder sb = new StringBuilder();
242         for (final Filter filter : filters) {
243             if (sb.length() == 0) {
244                 sb.append('{');
245             } else {
246                 sb.append(", ");
247             }
248             sb.append(filter.toString());
249         }
250         if (sb.length() > 0) {
251             sb.append('}');
252         }
253         return sb.toString();
254     }
255 
256     /**
257      * Create a CompositeFilter.
258      *
259      * @param filters
260      *            An array of Filters to call.
261      * @return The CompositeFilter.
262      */
263     @PluginFactory
264     public static CompositeFilter createFilters(@PluginElement("Filters") final Filter[] filters) {
265         final List<Filter> filterList = filters == null || filters.length == 0 ?
266             new ArrayList<Filter>() : Arrays.asList(filters);
267         return new CompositeFilter(filterList);
268     }
269 
270 }