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 java.util.Locale;
20  
21  import org.apache.logging.log4j.Level;
22  import org.apache.logging.log4j.Marker;
23  import org.apache.logging.log4j.core.LogEvent;
24  import org.apache.logging.log4j.core.Logger;
25  import org.apache.logging.log4j.core.config.plugins.Plugin;
26  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
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", type = "Core", elementType = "filter", printObject = true)
39  public final class ThresholdFilter extends AbstractFilter {
40  
41      private final Level level;
42  
43      private ThresholdFilter(Level level, Result onMatch, Result onMismatch) {
44          super(onMatch, onMismatch);
45          this.level = level;
46      }
47  
48      @Override
49      public Result filter(Logger logger, Level level, Marker marker, String msg, Object[] params) {
50          return filter(level);
51      }
52  
53      @Override
54      public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) {
55          return filter(level);
56      }
57  
58      @Override
59      public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) {
60          return filter(level);
61      }
62  
63      @Override
64      public Result filter(LogEvent event) {
65          return filter(event.getLevel());
66      }
67  
68      private Result filter(Level level) {
69          return level.isAtLeastAsSpecificAs(this.level) ? onMatch : onMismatch;
70      }
71  
72      @Override
73      public String toString() {
74          return level.toString();
75      }
76  
77      /**
78       * Create a ThresholdFilter.
79       * @param levelName The log Level.
80       * @param match The action to take on a match.
81       * @param mismatch The action to take on a mismatch.
82       * @return The created ThresholdFilter.
83       */
84      @PluginFactory
85      public static ThresholdFilter createFilter(@PluginAttr("level") String levelName,
86                                                 @PluginAttr("onMatch") String match,
87                                                 @PluginAttr("onMismatch") String mismatch) {
88          Level level = Level.toLevel(levelName, Level.ERROR);
89          Result onMatch = Result.toResult(match, Result.NEUTRAL);
90          Result onMismatch = Result.toResult(mismatch, Result.DENY);
91          return new ThresholdFilter(level, onMatch, onMismatch);
92      }
93  
94  }