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 org.apache.logging.log4j.Level;
20  import org.apache.logging.log4j.Marker;
21  import org.apache.logging.log4j.core.AbstractLifeCycle;
22  import org.apache.logging.log4j.core.Filter;
23  import org.apache.logging.log4j.core.LogEvent;
24  import org.apache.logging.log4j.core.Logger;
25  import org.apache.logging.log4j.message.Message;
26  
27  /**
28   * Users should extend this class to implement filters. Filters can be either context wide or attached to
29   * an appender. A filter may choose to support being called only from the context or only from an appender in
30   * which case it will only implement the required method(s). The rest will default to return {@link Result#NEUTRAL}.
31   * <p>
32   * Garbage-free note: the methods with unrolled varargs by default delegate to the
33   * {@link #filter(Logger, Level, Marker, String, Object...) filter method with vararg parameters}.
34   * Subclasses that want to be garbage-free should override these methods to implement the appropriate filtering
35   * without creating a vararg array.
36   * </p>
37   */
38  public abstract class AbstractFilter extends AbstractLifeCycle implements Filter {
39  
40      /**
41       * The onMatch Result.
42       */
43      protected final Result onMatch;
44  
45      /**
46       * The onMismatch Result.
47       */
48      protected final Result onMismatch;
49  
50      /**
51       * The default constructor.
52       */
53      protected AbstractFilter() {
54          this(null, null);
55      }
56  
57      /**
58       * Constructor that allows the onMatch and onMismatch actions to be set.
59       * @param onMatch The result to return when a match occurs.
60       * @param onMismatch The result to return when a match dos not occur.
61       */
62      protected AbstractFilter(final Result onMatch, final Result onMismatch) {
63          this.onMatch = onMatch == null ? Result.NEUTRAL : onMatch;
64          this.onMismatch = onMismatch == null ? Result.DENY : onMismatch;
65      }
66  
67      @Override
68      protected boolean equalsImpl(final Object obj) {
69          if (this == obj) {
70              return true;
71          }
72          if (!super.equalsImpl(obj)) {
73              return false;
74          }
75          if (getClass() != obj.getClass()) {
76              return false;
77          }
78          final AbstractFilter other = (AbstractFilter) obj;
79          if (onMatch != other.onMatch) {
80              return false;
81          }
82          if (onMismatch != other.onMismatch) {
83              return false;
84          }
85          return true;
86      }
87  
88      /**
89       * Context Filter method. The default returns NEUTRAL.
90       * @param event The LogEvent.
91       * @return The Result of filtering.
92       */
93      @Override
94      public Result filter(final LogEvent event) {
95          return Result.NEUTRAL;
96      }
97  
98      /**
99       * Appender Filter method. The default returns NEUTRAL.
100      * @param logger the Logger.
101      * @param level The logging Level.
102      * @param marker The Marker, if any.
103      * @param msg The message, if present.
104      * @param t A throwable or null.
105      * @return The Result of filtering.
106      */
107     @Override
108     public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
109                          final Throwable t) {
110         return Result.NEUTRAL;
111     }
112 
113     /**
114      * Appender Filter method. The default returns NEUTRAL.
115      * @param logger the Logger.
116      * @param level The logging Level.
117      * @param marker The Marker, if any.
118      * @param msg The message, if present.
119      * @param t A throwable or null.
120      * @return The Result of filtering.
121      */
122     @Override
123     public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
124                          final Throwable t) {
125         return Result.NEUTRAL;
126     }
127 
128     /**
129      * Appender Filter method. The default returns NEUTRAL.
130      * @param logger the Logger.
131      * @param level The logging Level.
132      * @param marker The Marker, if any.
133      * @param msg The message, if present.
134      * @param params An array of parameters or null.
135      * @return The Result of filtering.
136      */
137     @Override
138     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
139                          final Object... params) {
140         return Result.NEUTRAL;
141     }
142 
143     /**
144      * Appender Filter method. The default returns NEUTRAL.
145      * @param logger the Logger.
146      * @param level The logging Level.
147      * @param marker The Marker, if any.
148      * @param msg The message, if present.
149      * @param p0 the message parameters
150      * @return The Result of filtering.
151      * @since 2.7
152      */
153     @Override
154     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
155             final Object p0) {
156         return filter(logger, level, marker, msg, new Object[] {p0});
157     }
158 
159     /**
160      * Appender Filter method. The default returns NEUTRAL.
161      * @param logger the Logger.
162      * @param level The logging Level.
163      * @param marker The Marker, if any.
164      * @param msg The message, if present.
165      * @param p0 the message parameters
166      * @param p1 the message parameters
167      * @return The Result of filtering.
168      * @since 2.7
169      */
170     @Override
171     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
172             final Object p0, final Object p1) {
173         return filter(logger, level, marker, msg, new Object[] {p0, p1});
174     }
175 
176     /**
177      * Appender Filter method. The default returns NEUTRAL.
178      * @param logger the Logger.
179      * @param level The logging Level.
180      * @param marker The Marker, if any.
181      * @param msg The message, if present.
182      * @param p0 the message parameters
183      * @param p1 the message parameters
184      * @param p2 the message parameters
185      * @return The Result of filtering.
186      * @since 2.7
187      */
188     @Override
189     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
190             final Object p0, final Object p1, final Object p2) {
191         return filter(logger, level, marker, msg, new Object[] {p0, p1, p2});
192     }
193 
194     /**
195      * Appender Filter method. The default returns NEUTRAL.
196      * @param logger the Logger.
197      * @param level The logging Level.
198      * @param marker The Marker, if any.
199      * @param msg The message, if present.
200      * @param p0 the message parameters
201      * @param p1 the message parameters
202      * @param p2 the message parameters
203      * @param p3 the message parameters
204      * @return The Result of filtering.
205      * @since 2.7
206      */
207     @Override
208     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
209             final Object p0, final Object p1, final Object p2, final Object p3) {
210         return filter(logger, level, marker, msg, new Object[] {p0, p1, p2, p3});
211     }
212 
213     /**
214      * Appender Filter method. The default returns NEUTRAL.
215      * @param logger the Logger.
216      * @param level The logging Level.
217      * @param marker The Marker, if any.
218      * @param msg The message, if present.
219      * @param p0 the message parameters
220      * @param p1 the message parameters
221      * @param p2 the message parameters
222      * @param p3 the message parameters
223      * @param p4 the message parameters
224      * @return The Result of filtering.
225      * @since 2.7
226      */
227     @Override
228     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
229             final Object p0, final Object p1, final Object p2, final Object p3,
230             final Object p4) {
231         return filter(logger, level, marker, msg, new Object[] {p0, p1, p2, p3, p4});
232     }
233 
234     /**
235      * Appender Filter method. The default returns NEUTRAL.
236      * @param logger the Logger.
237      * @param level The logging Level.
238      * @param marker The Marker, if any.
239      * @param msg The message, if present.
240      * @param p0 the message parameters
241      * @param p1 the message parameters
242      * @param p2 the message parameters
243      * @param p3 the message parameters
244      * @param p4 the message parameters
245      * @param p5 the message parameters
246      * @return The Result of filtering.
247      * @since 2.7
248      */
249     @Override
250     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
251             final Object p0, final Object p1, final Object p2, final Object p3,
252             final Object p4, final Object p5) {
253         return filter(logger, level, marker, msg, new Object[] {p0, p1, p2, p3, p4, p5});
254     }
255 
256     /**
257      * Appender Filter method. The default returns NEUTRAL.
258      * @param logger the Logger.
259      * @param level The logging Level.
260      * @param marker The Marker, if any.
261      * @param msg The message, if present.
262      * @param p0 the message parameters
263      * @param p1 the message parameters
264      * @param p2 the message parameters
265      * @param p3 the message parameters
266      * @param p4 the message parameters
267      * @param p5 the message parameters
268      * @param p6 the message parameters
269      * @return The Result of filtering.
270      * @since 2.7
271      */
272     @Override
273     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
274             final Object p0, final Object p1, final Object p2, final Object p3,
275             final Object p4, final Object p5, final Object p6) {
276         return filter(logger, level, marker, msg, new Object[] {p0, p1, p2, p3, p4, p5, p6});
277     }
278 
279     /**
280      * Appender Filter method. The default returns NEUTRAL.
281      * @param logger the Logger.
282      * @param level The logging Level.
283      * @param marker The Marker, if any.
284      * @param msg The message, if present.
285      * @param p0 the message parameters
286      * @param p1 the message parameters
287      * @param p2 the message parameters
288      * @param p3 the message parameters
289      * @param p4 the message parameters
290      * @param p5 the message parameters
291      * @param p6 the message parameters
292      * @param p7 the message parameters
293      * @return The Result of filtering.
294      * @since 2.7
295      */
296     @Override
297     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
298             final Object p0, final Object p1, final Object p2, final Object p3,
299             final Object p4, final Object p5, final Object p6,
300             final Object p7) {
301         return filter(logger, level, marker, msg, new Object[] {p0, p1, p2, p3, p4, p5, p6, p7});
302     }
303 
304     /**
305      * Appender Filter method. The default returns NEUTRAL.
306      * @param logger the Logger.
307      * @param level The logging Level.
308      * @param marker The Marker, if any.
309      * @param msg The message, if present.
310      * @param p0 the message parameters
311      * @param p1 the message parameters
312      * @param p2 the message parameters
313      * @param p3 the message parameters
314      * @param p4 the message parameters
315      * @param p5 the message parameters
316      * @param p6 the message parameters
317      * @param p7 the message parameters
318      * @param p8 the message parameters
319      * @return The Result of filtering.
320      * @since 2.7
321      */
322     @Override
323     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
324             final Object p0, final Object p1, final Object p2, final Object p3,
325             final Object p4, final Object p5, final Object p6,
326             final Object p7, final Object p8) {
327         return filter(logger, level, marker, msg, new Object[] {p0, p1, p2, p3, p4, p5, p6, p7, p8});
328     }
329 
330     /**
331      * Appender Filter method. The default returns NEUTRAL.
332      * @param logger the Logger.
333      * @param level The logging Level.
334      * @param marker The Marker, if any.
335      * @param msg The message, if present.
336      * @param p0 the message parameters
337      * @param p1 the message parameters
338      * @param p2 the message parameters
339      * @param p3 the message parameters
340      * @param p4 the message parameters
341      * @param p5 the message parameters
342      * @param p6 the message parameters
343      * @param p7 the message parameters
344      * @param p8 the message parameters
345      * @param p9 the message parameters
346      * @return The Result of filtering.
347      * @since 2.7
348      */
349     @Override
350     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
351             final Object p0, final Object p1, final Object p2, final Object p3,
352             final Object p4, final Object p5, final Object p6,
353             final Object p7, final Object p8, final Object p9) {
354         return filter(logger, level, marker, msg, new Object[] {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9});
355     }
356 
357     /**
358      * Returns the Result to be returned when a match occurs.
359      * @return the onMatch Result.
360      */
361     @Override
362     public final Result getOnMatch() {
363         return onMatch;
364     }
365 
366     /**
367      * Returns the Result to be returned when a match does not occur.
368      * @return the onMismatch Result.
369      */
370     @Override
371     public final Result getOnMismatch() {
372         return onMismatch;
373     }
374 
375     @Override
376     protected int hashCodeImpl() {
377         final int prime = 31;
378         int result = super.hashCodeImpl();
379         result = prime * result + ((onMatch == null) ? 0 : onMatch.hashCode());
380         result = prime * result + ((onMismatch == null) ? 0 : onMismatch.hashCode());
381         return result;
382     }
383 
384     @Override
385     public String toString() {
386         return this.getClass().getSimpleName();
387     }
388 }