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 NEUTRAL.
031 *
032 */
033public abstract class AbstractFilter extends AbstractLifeCycle implements Filter {
034
035    /**
036     * The onMatch Result.
037     */
038    protected final Result onMatch;
039
040    /**
041     * The onMismatch Result.
042     */
043    protected final Result onMismatch;
044
045    /**
046     * The default constructor.
047     */
048    protected AbstractFilter() {
049        this(null, null);
050    }
051
052    /**
053     * Constructor that allows the onMatch and onMismatch actions to be set.
054     * @param onMatch The result to return when a match occurs.
055     * @param onMismatch The result to return when a match dos not occur.
056     */
057    protected AbstractFilter(final Result onMatch, final Result onMismatch) {
058        this.onMatch = onMatch == null ? Result.NEUTRAL : onMatch;
059        this.onMismatch = onMismatch == null ? Result.DENY : onMismatch;
060    }
061
062    @Override
063    protected boolean equalsImpl(final Object obj) {
064        if (this == obj) {
065            return true;
066        }
067        if (!super.equalsImpl(obj)) {
068            return false;
069        }
070        if (getClass() != obj.getClass()) {
071            return false;
072        }
073        final AbstractFilter other = (AbstractFilter) obj;
074        if (onMatch != other.onMatch) {
075            return false;
076        }
077        if (onMismatch != other.onMismatch) {
078            return false;
079        }
080        return true;
081    }
082
083    /**
084     * Context Filter method. The default returns NEUTRAL.
085     * @param event The LogEvent.
086     * @return The Result of filtering.
087     */
088    @Override
089    public Result filter(final LogEvent event) {
090        return Result.NEUTRAL;
091    }
092
093    /**
094     * Appender Filter method. The default returns NEUTRAL.
095     * @param logger the Logger.
096     * @param level The logging Level.
097     * @param marker The Marker, if any.
098     * @param msg The message, if present.
099     * @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}