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    private static final long serialVersionUID = 1L;
036
037    /**
038     * The onMatch Result.
039     */
040    protected final Result onMatch;
041
042    /**
043     * The onMismatch Result.
044     */
045    protected final Result onMismatch;
046
047    /**
048     * The default constructor.
049     */
050    protected AbstractFilter() {
051        this(null, null);
052    }
053
054    /**
055     * Constructor that allows the onMatch and onMismatch actions to be set.
056     * @param onMatch The result to return when a match occurs.
057     * @param onMismatch The result to return when a match dos not occur.
058     */
059    protected AbstractFilter(final Result onMatch, final Result onMismatch) {
060        this.onMatch = onMatch == null ? Result.NEUTRAL : onMatch;
061        this.onMismatch = onMismatch == null ? Result.DENY : onMismatch;
062    }
063
064    protected boolean equalsImpl(final Object obj) {
065        if (this == obj) {
066            return true;
067        }
068        if (!super.equalsImpl(obj)) {
069            return false;
070        }
071        if (getClass() != obj.getClass()) {
072            return false;
073        }
074        final AbstractFilter other = (AbstractFilter) obj;
075        if (onMatch != other.onMatch) {
076            return false;
077        }
078        if (onMismatch != other.onMismatch) {
079            return false;
080        }
081        return true;
082    }
083
084    /**
085     * Context Filter method. The default returns NEUTRAL.
086     * @param event The LogEvent.
087     * @return The Result of filtering.
088     */
089    @Override
090    public Result filter(final LogEvent event) {
091        return Result.NEUTRAL;
092    }
093
094    /**
095     * Appender Filter method. The default returns NEUTRAL.
096     * @param logger the Logger.
097     * @param level The logging Level.
098     * @param marker The Marker, if any.
099     * @param msg The message, if present.
100     * @param t A throwable or null.
101     * @return The Result of filtering.
102     */
103    @Override
104    public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
105                         final Throwable t) {
106        return Result.NEUTRAL;
107    }
108
109    /**
110     * Appender Filter method. The default returns NEUTRAL.
111     * @param logger the Logger.
112     * @param level The logging Level.
113     * @param marker The Marker, if any.
114     * @param msg The message, if present.
115     * @param t A throwable or null.
116     * @return The Result of filtering.
117     */
118    @Override
119    public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
120                         final Throwable t) {
121        return Result.NEUTRAL;
122    }
123
124    /**
125     * Appender Filter method. The default returns NEUTRAL.
126     * @param logger the Logger.
127     * @param level The logging Level.
128     * @param marker The Marker, if any.
129     * @param msg The message, if present.
130     * @param params An array of parameters or null.
131     * @return The Result of filtering.
132     */
133    @Override
134    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
135                         final Object... params) {
136        return Result.NEUTRAL;
137    }
138
139    /**
140     * Returns the Result to be returned when a match occurs.
141     * @return the onMatch Result.
142     */
143    @Override
144    public final Result getOnMatch() {
145        return onMatch;
146    }
147
148    /**
149     * Returns the Result to be returned when a match does not occur.
150     * @return the onMismatch Result.
151     */
152    @Override
153    public final Result getOnMismatch() {
154        return onMismatch;
155    }
156
157    protected int hashCodeImpl() {
158        final int prime = 31;
159        int result = super.hashCodeImpl();
160        result = prime * result + ((onMatch == null) ? 0 : onMatch.hashCode());
161        result = prime * result + ((onMismatch == null) ? 0 : onMismatch.hashCode());
162        return result;
163    }
164
165    @Override
166    public String toString() {
167        return this.getClass().getSimpleName();
168    }
169}