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