001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.filter;
018
019import org.apache.logging.log4j.Level;
020import org.apache.logging.log4j.Marker;
021import org.apache.logging.log4j.core.AbstractLifeCycle;
022import org.apache.logging.log4j.core.Filter;
023import org.apache.logging.log4j.core.LogEvent;
024import org.apache.logging.log4j.core.Logger;
025import org.apache.logging.log4j.message.Message;
026
027/**
028 * Users should extend this class to implement filters. Filters can be either context wide or attached to
029 * an appender. A filter may choose to support being called only from the context or only from an appender in
030 * which case it will only implement the required method(s). The rest will default to return {@link Result#NEUTRAL}.
031 * <p>
032 * Garbage-free note: the methods with unrolled varargs by default delegate to the
033 * {@link #filter(Logger, Level, Marker, String, Object...) filter method with vararg parameters}.
034 * Subclasses that want to be garbage-free should override these methods to implement the appropriate filtering
035 * without creating a vararg array.
036 * </p>
037 */
038public abstract class AbstractFilter extends AbstractLifeCycle implements Filter {
039
040    /**
041     * The onMatch Result.
042     */
043    protected final Result onMatch;
044
045    /**
046     * The onMismatch Result.
047     */
048    protected final Result onMismatch;
049
050    /**
051     * The default constructor.
052     */
053    protected AbstractFilter() {
054        this(null, null);
055    }
056
057    /**
058     * Constructor that allows the onMatch and onMismatch actions to be set.
059     * @param onMatch The result to return when a match occurs.
060     * @param onMismatch The result to return when a match dos not occur.
061     */
062    protected AbstractFilter(final Result onMatch, final Result onMismatch) {
063        this.onMatch = onMatch == null ? Result.NEUTRAL : onMatch;
064        this.onMismatch = onMismatch == null ? Result.DENY : onMismatch;
065    }
066
067    @Override
068    protected boolean equalsImpl(final Object obj) {
069        if (this == obj) {
070            return true;
071        }
072        if (!super.equalsImpl(obj)) {
073            return false;
074        }
075        if (getClass() != obj.getClass()) {
076            return false;
077        }
078        final AbstractFilter other = (AbstractFilter) obj;
079        if (onMatch != other.onMatch) {
080            return false;
081        }
082        if (onMismatch != other.onMismatch) {
083            return false;
084        }
085        return true;
086    }
087
088    /**
089     * Context Filter method. The default returns NEUTRAL.
090     * @param event The LogEvent.
091     * @return The Result of filtering.
092     */
093    @Override
094    public Result filter(final LogEvent event) {
095        return Result.NEUTRAL;
096    }
097
098    /**
099     * 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}