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.Filter;
28  import org.apache.logging.log4j.core.LifeCycle;
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 implements Iterable<Filter>, Filter, LifeCycle {
41  
42      private final List<Filter> filters;
43      private final boolean hasFilters;
44  
45      private boolean isStarted;
46  
47      private CompositeFilter() {
48          this.filters = new ArrayList<Filter>();
49          this.hasFilters = false;
50      }
51  
52      private CompositeFilter(final List<Filter> filters) {
53          if (filters == null) {
54              this.filters = Collections.unmodifiableList(new ArrayList<Filter>());
55              this.hasFilters = false;
56              return;
57          }
58          this.filters = Collections.unmodifiableList(filters);
59          this.hasFilters = this.filters.size() > 0;
60      }
61  
62      public CompositeFilter addFilter(final Filter filter) {
63          final List<Filter> filters = new ArrayList<Filter>(this.filters);
64          filters.add(filter);
65          return new CompositeFilter(Collections.unmodifiableList(filters));
66      }
67  
68      public CompositeFilter removeFilter(final Filter filter) {
69          final List<Filter> filters = new ArrayList<Filter>(this.filters);
70          filters.remove(filter);
71          return new CompositeFilter(Collections.unmodifiableList(filters));
72      }
73  
74      @Override
75      public Iterator<Filter> iterator() {
76          return filters.iterator();
77      }
78  
79      public List<Filter> getFilters() {
80          return filters;
81      }
82  
83      public boolean hasFilters() {
84          return hasFilters;
85      }
86  
87      public int size() {
88          return filters.size();
89      }
90  
91      @Override
92      public void start() {
93          for (final Filter filter : filters) {
94              if (filter instanceof LifeCycle) {
95                  ((LifeCycle) filter).start();
96              }
97          }
98          isStarted = true;
99      }
100 
101     @Override
102     public void stop() {
103         for (final Filter filter : filters) {
104             if (filter instanceof LifeCycle) {
105                 ((LifeCycle) filter).stop();
106             }
107         }
108         isStarted = false;
109     }
110 
111     @Override
112     public boolean isStarted() {
113         return isStarted;
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> f = filters == null || filters.length == 0 ?
266             new ArrayList<Filter>() : Arrays.asList(filters);
267         return new CompositeFilter(f);
268     }
269 
270 }