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.Filter;
    22  import org.apache.logging.log4j.core.LogEvent;
    23  import org.apache.logging.log4j.core.Logger;
    24  import org.apache.logging.log4j.core.config.Node;
    25  import org.apache.logging.log4j.core.config.plugins.Plugin;
    26  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
    27  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
    28  import org.apache.logging.log4j.message.Message;
    29
    30  /**
    31   * This filter returns the onMatch result if the level in the LogEvent is the same or more specific
    32   * than the configured level and the onMismatch value otherwise. For example, if the ThresholdFilter
    33   * is configured with Level ERROR and the LogEvent contains Level DEBUG then the onMismatch value will
    34   * be returned since ERROR events are more specific than DEBUG.
    35   *
    36   * The default Level is ERROR.
    37   */
    38  @Plugin(name = "ThresholdFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
    39  public final class ThresholdFilter extends AbstractFilter {
    40
    41      private static final long serialVersionUID = 1L;
    42
    43      private final Level level;
    44
    45      private ThresholdFilter(final Level level, final Result onMatch, final Result onMismatch) {
    46          super(onMatch, onMismatch);
    47          this.level = level;
    48      }
    49
    50      @Override
    51      public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
    52                           final Object... params) {
    53          return filter(level);
    54      }
    55
    56      @Override
    57      public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
    58                           final Throwable t) {
    59          return filter(level);
    60      }
    61
    62      @Override
    63      public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
    64                           final Throwable t) {
    65          return filter(level);
    66      }
    67
    68      @Override
    69      public Result filter(final LogEvent event) {
    70          return filter(event.getLevel());
    71      }
    72
    73      private Result filter(final Level level) {
    74          return level.isMoreSpecificThan(this.level) ? onMatch : onMismatch;
    75      }
    76
    77      @Override
    78      public String toString() {
    79          return level.toString();
    80      }
    81
    82      /**
    83       * Create a ThresholdFilter.
    84       * @param level The log Level.
    85       * @param match The action to take on a match.
    86       * @param mismatch The action to take on a mismatch.
    87       * @return The created ThresholdFilter.
    88       */
    89      @PluginFactory
    90      public static ThresholdFilter createFilter(
    91              @PluginAttribute("level") final Level level,
    92              @PluginAttribute("onMatch") final Result match,
    93              @PluginAttribute("onMismatch") final Result mismatch) {
    94          final Level actualLevel = level == null ? Level.ERROR : level;
    95          final Result onMatch = match == null ? Result.NEUTRAL : match;
    96          final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
    97          return new ThresholdFilter(actualLevel, onMatch, onMismatch);
    98      }
    99
    100 }