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