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     */
017    package org.apache.logging.log4j.core.filter;
018    
019    import org.apache.logging.log4j.Level;
020    import org.apache.logging.log4j.Marker;
021    import org.apache.logging.log4j.core.Filter;
022    import org.apache.logging.log4j.core.LifeCycle;
023    import org.apache.logging.log4j.core.LogEvent;
024    import org.apache.logging.log4j.core.Logger;
025    import org.apache.logging.log4j.status.StatusLogger;
026    import org.apache.logging.log4j.message.Message;
027    
028    /**
029     * Users should extend this class to implement filters. Filters can be either context wide or attached to
030     * an appender. A filter may choose to support being called only from the context or only from an appender in
031     * which case it will only implement the required method(s). The rest will default to return NEUTRAL.
032     *
033     */
034    public abstract class AbstractFilter implements Filter, LifeCycle {
035        /**
036         * Allow subclasses access to the status logger without creating another instance.
037         */
038        protected static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
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        private boolean started;
051    
052        /**
053         * The default constructor.
054         */
055        protected AbstractFilter() {
056            this(null, null);
057        }
058    
059        /**
060         * Constructor that allows the onMatch and onMismatch actions to be set.
061         * @param onMatch The result to return when a match occurs.
062         * @param onMismatch The result to return when a match dos not occur.
063         */
064        protected AbstractFilter(final Result onMatch, final Result onMismatch) {
065            this.onMatch = onMatch == null ? Result.NEUTRAL : onMatch;
066            this.onMismatch = onMismatch == null ? Result.DENY : onMismatch;
067        }
068    
069        /**
070         * Mark the Filter as started.
071         */
072        @Override
073        public void start() {
074            started = true;
075        }
076    
077        /**
078         * Determine if the the Filter has started.
079         * @return true if the Filter is started, false otherwise.
080         */
081        @Override
082        public boolean isStarted() {
083            return started;
084        }
085    
086        /**
087         * Mark the Filter as stopped.
088         */
089        @Override
090        public void stop() {
091            started = false;
092        }
093    
094        /**
095         * Returns the Result to be returned when a match does not occur.
096         * @return the onMismatch Result.
097         */
098        @Override
099        public final Result getOnMismatch() {
100            return onMismatch;
101        }
102    
103        /**
104         * Returns the Result to be returned when a match occurs.
105         * @return the onMatch Result.
106         */
107        @Override
108        public final Result getOnMatch() {
109            return onMatch;
110        }
111    
112        @Override
113        public String toString() {
114            return this.getClass().getSimpleName();
115        }
116    
117        /**
118         * Appender Filter method. The default returns NEUTRAL.
119         * @param logger the Logger.
120         * @param level The logging Level.
121         * @param marker The Marker, if any.
122         * @param msg The message, if present.
123         * @param params An array of parameters or null.
124         * @return The Result of filtering.
125         */
126        @Override
127        public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
128                             final Object... params) {
129            return Result.NEUTRAL;
130        }
131    
132        /**
133         * Appender Filter method. The default returns NEUTRAL.
134         * @param logger the Logger.
135         * @param level The logging Level.
136         * @param marker The Marker, if any.
137         * @param msg The message, if present.
138         * @param t A throwable or null.
139         * @return The Result of filtering.
140         */
141        @Override
142        public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
143                             final Throwable t) {
144            return Result.NEUTRAL;
145        }
146    
147        /**
148         * Appender Filter method. The default returns NEUTRAL.
149         * @param logger the Logger.
150         * @param level The logging Level.
151         * @param marker The Marker, if any.
152         * @param msg The message, if present.
153         * @param t A throwable or null.
154         * @return The Result of filtering.
155         */
156        @Override
157        public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
158                             final Throwable t) {
159            return Result.NEUTRAL;
160        }
161    
162        /**
163         * Context Filter method. The default returns NEUTRAL.
164         * @param event The LogEvent.
165         * @return The Result of filtering.
166         */
167        @Override
168        public Result filter(final LogEvent event) {
169            return Result.NEUTRAL;
170        }
171    }