View Javadoc
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.AbstractLifeCycle;
22  import org.apache.logging.log4j.core.Filter;
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  
27  /**
28   * Users should extend this class to implement filters. Filters can be either context wide or attached to
29   * an appender. A filter may choose to support being called only from the context or only from an appender in
30   * which case it will only implement the required method(s). The rest will default to return NEUTRAL.
31   *
32   */
33  public abstract class AbstractFilter extends AbstractLifeCycle implements Filter {
34      
35      private static final long serialVersionUID = 1L;
36  
37      /**
38       * The onMatch Result.
39       */
40      protected final Result onMatch;
41  
42      /**
43       * The onMismatch Result.
44       */
45      protected final Result onMismatch;
46  
47      /**
48       * The default constructor.
49       */
50      protected AbstractFilter() {
51          this(null, null);
52      }
53  
54      /**
55       * Constructor that allows the onMatch and onMismatch actions to be set.
56       * @param onMatch The result to return when a match occurs.
57       * @param onMismatch The result to return when a match dos not occur.
58       */
59      protected AbstractFilter(final Result onMatch, final Result onMismatch) {
60          this.onMatch = onMatch == null ? Result.NEUTRAL : onMatch;
61          this.onMismatch = onMismatch == null ? Result.DENY : onMismatch;
62      }
63  
64      @Override
65      public boolean equals(final Object obj) {
66          if (this == obj) {
67              return true;
68          }
69          if (!super.equals(obj)) {
70              return false;
71          }
72          if (getClass() != obj.getClass()) {
73              return false;
74          }
75          final AbstractFilter other = (AbstractFilter) obj;
76          if (onMatch != other.onMatch) {
77              return false;
78          }
79          if (onMismatch != other.onMismatch) {
80              return false;
81          }
82          return true;
83      }
84  
85      /**
86       * Context Filter method. The default returns NEUTRAL.
87       * @param event The LogEvent.
88       * @return The Result of filtering.
89       */
90      @Override
91      public Result filter(final LogEvent event) {
92          return Result.NEUTRAL;
93      }
94  
95      /**
96       * Appender Filter method. The default returns NEUTRAL.
97       * @param logger the Logger.
98       * @param level The logging Level.
99       * @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     public int hashCode() {
160         final int prime = 31;
161         int result = super.hashCode();
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 }