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 final Level level;
42  
43      private ThresholdFilter(final Level level, final Result onMatch, final Result onMismatch) {
44          super(onMatch, onMismatch);
45          this.level = level;
46      }
47  
48      @Override
49      public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
50                           final Object... params) {
51          return filter(level);
52      }
53  
54      @Override
55      public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
56                           final Throwable t) {
57          return filter(level);
58      }
59  
60      @Override
61      public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
62                           final Throwable t) {
63          return filter(level);
64      }
65  
66      @Override
67      public Result filter(final LogEvent event) {
68          return filter(event.getLevel());
69      }
70  
71      private Result filter(final Level level) {
72          return level.isMoreSpecificThan(this.level) ? onMatch : onMismatch;
73      }
74  
75      public Level getLevel() {
76          return level;
77      }
78  
79      @Override
80      public String toString() {
81          return level.toString();
82      }
83  
84      /**
85       * Create a ThresholdFilter.
86       * @param level The log Level.
87       * @param match The action to take on a match.
88       * @param mismatch The action to take on a mismatch.
89       * @return The created ThresholdFilter.
90       */
91      @PluginFactory
92      public static ThresholdFilter createFilter(
93              @PluginAttribute("level") final Level level,
94              @PluginAttribute("onMatch") final Result match,
95              @PluginAttribute("onMismatch") final Result mismatch) {
96          final Level actualLevel = level == null ? Level.ERROR : level;
97          final Result onMatch = match == null ? Result.NEUTRAL : match;
98          final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
99          return new ThresholdFilter(actualLevel, onMatch, onMismatch);
100     }
101 
102 }