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    /**
063     * Returns the Result to be returned when a match does not occur.
064     * @return the onMismatch Result.
065     */
066    @Override
067    public final Result getOnMismatch() {
068        return onMismatch;
069    }
070
071    /**
072     * Returns the Result to be returned when a match occurs.
073     * @return the onMatch Result.
074     */
075    @Override
076    public final Result getOnMatch() {
077        return onMatch;
078    }
079
080    @Override
081    public String toString() {
082        return this.getClass().getSimpleName();
083    }
084
085    /**
086     * Appender Filter method. The default returns NEUTRAL.
087     * @param logger the Logger.
088     * @param level The logging Level.
089     * @param marker The Marker, if any.
090     * @param msg The message, if present.
091     * @param params An array of parameters or null.
092     * @return The Result of filtering.
093     */
094    @Override
095    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
096                         final Object... params) {
097        return Result.NEUTRAL;
098    }
099
100    /**
101     * Appender Filter method. The default returns NEUTRAL.
102     * @param logger the Logger.
103     * @param level The logging Level.
104     * @param marker The Marker, if any.
105     * @param msg The message, if present.
106     * @param t A throwable or null.
107     * @return The Result of filtering.
108     */
109    @Override
110    public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
111                         final Throwable t) {
112        return Result.NEUTRAL;
113    }
114
115    /**
116     * Appender Filter method. The default returns NEUTRAL.
117     * @param logger the Logger.
118     * @param level The logging Level.
119     * @param marker The Marker, if any.
120     * @param msg The message, if present.
121     * @param t A throwable or null.
122     * @return The Result of filtering.
123     */
124    @Override
125    public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
126                         final Throwable t) {
127        return Result.NEUTRAL;
128    }
129
130    /**
131     * Context Filter method. The default returns NEUTRAL.
132     * @param event The LogEvent.
133     * @return The Result of filtering.
134     */
135    @Override
136    public Result filter(final LogEvent event) {
137        return Result.NEUTRAL;
138    }
139}