1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache license, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the license for the specific language governing permissions and 15 * limitations under the license. 16 */ 17 package org.apache.logging.log4j.core.filter; 18 19 import org.apache.logging.log4j.Level; 20 import org.apache.logging.log4j.Marker; 21 import org.apache.logging.log4j.core.Filter; 22 import org.apache.logging.log4j.core.LifeCycle; 23 import org.apache.logging.log4j.core.LogEvent; 24 import org.apache.logging.log4j.core.Logger; 25 import org.apache.logging.log4j.message.Message; 26 import org.apache.logging.log4j.status.StatusLogger; 27 28 /** 29 * Users should extend this class to implement filters. Filters can be either context wide or attached to 30 * an appender. A filter may choose to support being called only from the context or only from an appender in 31 * which case it will only implement the required method(s). The rest will default to return NEUTRAL. 32 * 33 */ 34 public abstract class AbstractFilter implements Filter, LifeCycle { 35 36 /** 37 * Allow subclasses access to the status logger without creating another instance. 38 */ 39 protected static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger(); 40 41 /** 42 * The onMatch Result. 43 */ 44 protected final Result onMatch; 45 46 /** 47 * The onMismatch Result. 48 */ 49 protected final Result onMismatch; 50 51 private boolean started; 52 53 /** 54 * The default constructor. 55 */ 56 protected AbstractFilter() { 57 this(null, null); 58 } 59 60 /** 61 * Constructor that allows the onMatch and onMismatch actions to be set. 62 * @param onMatch The result to return when a match occurs. 63 * @param onMismatch The result to return when a match dos not occur. 64 */ 65 protected AbstractFilter(final Result onMatch, final Result onMismatch) { 66 this.onMatch = onMatch == null ? Result.NEUTRAL : onMatch; 67 this.onMismatch = onMismatch == null ? Result.DENY : onMismatch; 68 } 69 70 /** 71 * Mark the Filter as started. 72 */ 73 @Override 74 public void start() { 75 started = true; 76 } 77 78 /** 79 * Determine if the the Filter has started. 80 * @return true if the Filter is started, false otherwise. 81 */ 82 @Override 83 public boolean isStarted() { 84 return started; 85 } 86 87 /** 88 * Mark the Filter as stopped. 89 */ 90 @Override 91 public void stop() { 92 started = false; 93 } 94 95 /** 96 * Returns the Result to be returned when a match does not occur. 97 * @return the onMismatch Result. 98 */ 99 @Override 100 public final Result getOnMismatch() { 101 return onMismatch; 102 } 103 104 /** 105 * Returns the Result to be returned when a match occurs. 106 * @return the onMatch Result. 107 */ 108 @Override 109 public final Result getOnMatch() { 110 return onMatch; 111 } 112 113 @Override 114 public String toString() { 115 return this.getClass().getSimpleName(); 116 } 117 118 /** 119 * Appender Filter method. The default returns NEUTRAL. 120 * @param logger the Logger. 121 * @param level The logging Level. 122 * @param marker The Marker, if any. 123 * @param msg The message, if present. 124 * @param params An array of parameters or null. 125 * @return The Result of filtering. 126 */ 127 @Override 128 public Result filter(final Logger logger, final Level level, final Marker marker, final String msg, 129 final Object... params) { 130 return Result.NEUTRAL; 131 } 132 133 /** 134 * Appender Filter method. The default returns NEUTRAL. 135 * @param logger the Logger. 136 * @param level The logging Level. 137 * @param marker The Marker, if any. 138 * @param msg The message, if present. 139 * @param t A throwable or null. 140 * @return The Result of filtering. 141 */ 142 @Override 143 public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg, 144 final Throwable t) { 145 return Result.NEUTRAL; 146 } 147 148 /** 149 * Appender Filter method. The default returns NEUTRAL. 150 * @param logger the Logger. 151 * @param level The logging Level. 152 * @param marker The Marker, if any. 153 * @param msg The message, if present. 154 * @param t A throwable or null. 155 * @return The Result of filtering. 156 */ 157 @Override 158 public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg, 159 final Throwable t) { 160 return Result.NEUTRAL; 161 } 162 163 /** 164 * Context Filter method. The default returns NEUTRAL. 165 * @param event The LogEvent. 166 * @return The Result of filtering. 167 */ 168 @Override 169 public Result filter(final LogEvent event) { 170 return Result.NEUTRAL; 171 } 172 }