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