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