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