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.status.StatusLogger; 26 import org.apache.logging.log4j.message.Message; 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 * Allow subclasses access to the status logger without creating another instance. 37 */ 38 protected static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger(); 39 40 /** 41 * The onMatch Result. 42 */ 43 protected final Result onMatch; 44 45 /** 46 * The onMismatch Result. 47 */ 48 protected final Result onMismatch; 49 50 private boolean started; 51 52 /** 53 * The default constructor. 54 */ 55 protected AbstractFilter() { 56 this(null, null); 57 } 58 59 /** 60 * Constructor that allows the onMatch and onMismatch actions to be set. 61 * @param onMatch The result to return when a match occurs. 62 * @param onMismatch The result to return when a match dos not occur. 63 */ 64 protected AbstractFilter(final Result onMatch, final Result onMismatch) { 65 this.onMatch = onMatch == null ? Result.NEUTRAL : onMatch; 66 this.onMismatch = onMismatch == null ? Result.DENY : onMismatch; 67 } 68 69 /** 70 * Mark the Filter as started. 71 */ 72 public void start() { 73 started = true; 74 } 75 76 /** 77 * Determine if the the Filter has started. 78 * @return true if the Filter is started, false otherwise. 79 */ 80 public boolean isStarted() { 81 return started; 82 } 83 84 /** 85 * Mark the Filter as stopped. 86 */ 87 public void stop() { 88 started = false; 89 } 90 91 /** 92 * Returns the Result to be returned when a match does not occur. 93 * @return the onMismatch Result. 94 */ 95 public final Result getOnMismatch() { 96 return onMismatch; 97 } 98 99 /** 100 * Returns the Result to be returned when a match occurs. 101 * @return the onMatch Result. 102 */ 103 public final Result getOnMatch() { 104 return onMatch; 105 } 106 107 @Override 108 public String toString() { 109 return this.getClass().getSimpleName(); 110 } 111 112 /** 113 * Appender Filter method. The default returns NEUTRAL. 114 * @param logger the Logger. 115 * @param level The logging Level. 116 * @param marker The Marker, if any. 117 * @param msg The message, if present. 118 * @param params An array of parameters or null. 119 * @return The Result of filtering. 120 */ 121 public Result filter(final Logger logger, final Level level, final Marker marker, final String msg, 122 final Object... params) { 123 return Result.NEUTRAL; 124 } 125 126 /** 127 * Appender Filter method. The default returns NEUTRAL. 128 * @param logger the Logger. 129 * @param level The logging Level. 130 * @param marker The Marker, if any. 131 * @param msg The message, if present. 132 * @param t A throwable or null. 133 * @return The Result of filtering. 134 */ 135 public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg, 136 final Throwable t) { 137 return Result.NEUTRAL; 138 } 139 140 /** 141 * Appender Filter method. The default returns NEUTRAL. 142 * @param logger the Logger. 143 * @param level The logging Level. 144 * @param marker The Marker, if any. 145 * @param msg The message, if present. 146 * @param t A throwable or null. 147 * @return The Result of filtering. 148 */ 149 public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg, 150 final Throwable t) { 151 return Result.NEUTRAL; 152 } 153 154 /** 155 * Context Filter method. The default returns NEUTRAL. 156 * @param event The LogEvent. 157 * @return The Result of filtering. 158 */ 159 public Result filter(final LogEvent event) { 160 return Result.NEUTRAL; 161 } 162 }