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