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      protected boolean equalsImpl(final Object obj) {
    65          if (this == obj) {
    66              return true;
    67          }
    68          if (!super.equalsImpl(obj)) {
    69              return false;
    70          }
    71          if (getClass() != obj.getClass()) {
    72              return false;
    73          }
    74          final AbstractFilter other = (AbstractFilter) obj;
    75          if (onMatch != other.onMatch) {
    76              return false;
    77          }
    78          if (onMismatch != other.onMismatch) {
    79              return false;
    80          }
    81          return true;
    82      }
    83
    84      /**
    85       * Context Filter method. The default returns NEUTRAL.
    86       * @param event The LogEvent.
    87       * @return The Result of filtering.
    88       */
    89      @Override
    90      public Result filter(final LogEvent event) {
    91          return Result.NEUTRAL;
    92      }
    93
    94      /**
    95       * Appender Filter method. The default returns NEUTRAL.
    96       * @param logger the Logger.
    97       * @param level The logging Level.
    98       * @param marker The Marker, if any.
    99       * @param msg The message, if present.
    100      * @param t A throwable or null.
    101      * @return The Result of filtering.
    102      */
    103     @Override
    104     public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
    105                          final Throwable t) {
    106         return Result.NEUTRAL;
    107     }
    108
    109     /**
    110      * Appender Filter method. The default returns NEUTRAL.
    111      * @param logger the Logger.
    112      * @param level The logging Level.
    113      * @param marker The Marker, if any.
    114      * @param msg The message, if present.
    115      * @param t A throwable or null.
    116      * @return The Result of filtering.
    117      */
    118     @Override
    119     public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
    120                          final Throwable t) {
    121         return Result.NEUTRAL;
    122     }
    123
    124     /**
    125      * Appender Filter method. The default returns NEUTRAL.
    126      * @param logger the Logger.
    127      * @param level The logging Level.
    128      * @param marker The Marker, if any.
    129      * @param msg The message, if present.
    130      * @param params An array of parameters or null.
    131      * @return The Result of filtering.
    132      */
    133     @Override
    134     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
    135                          final Object... params) {
    136         return Result.NEUTRAL;
    137     }
    138
    139     /**
    140      * Returns the Result to be returned when a match occurs.
    141      * @return the onMatch Result.
    142      */
    143     @Override
    144     public final Result getOnMatch() {
    145         return onMatch;
    146     }
    147
    148     /**
    149      * Returns the Result to be returned when a match does not occur.
    150      * @return the onMismatch Result.
    151      */
    152     @Override
    153     public final Result getOnMismatch() {
    154         return onMismatch;
    155     }
    156
    157     protected int hashCodeImpl() {
    158         final int prime = 31;
    159         int result = super.hashCodeImpl();
    160         result = prime * result + ((onMatch == null) ? 0 : onMatch.hashCode());
    161         result = prime * result + ((onMismatch == null) ? 0 : onMismatch.hashCode());
    162         return result;
    163     }
    164
    165     @Override
    166     public String toString() {
    167         return this.getClass().getSimpleName();
    168     }
    169 }