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 in the
32   * range of the configured min and max levels, otherwise it returns onMismatch
33   * value . For example, if the filter is configured with Level ERROR and Level
34   * INFO and the LogEvent contains Level WARN then the onMatch value will be
35   * returned since WARN events are in between ERROR and INFO.
36   *
37   * The default Levels are both ERROR.
38   */
39  @Plugin(name = "LevelRangeFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
40  public final class LevelRangeFilter extends AbstractFilter {
41  
42      /**
43       * Create a ThresholdFilter.
44       * 
45       * @param minLevel
46       *            The minimum log Level.
47       * @param maxLevel
48       *            The maximum log Level.
49       * @param match
50       *            The action to take on a match.
51       * @param mismatch
52       *            The action to take on a mismatch.
53       * @return The created ThresholdFilter.
54       */
55      @PluginFactory
56      public static LevelRangeFilter createFilter(
57              // @formatter:off
58              @PluginAttribute("minLevel") final Level minLevel,
59              @PluginAttribute("maxLevel") final Level maxLevel,
60              @PluginAttribute("onMatch") final Result match, 
61              @PluginAttribute("onMismatch") final Result mismatch) {
62              // @formatter:on
63          final Level actualMinLevel = minLevel == null ? Level.ERROR : minLevel;
64          final Level actualMaxLevel = minLevel == null ? Level.ERROR : maxLevel;
65          final Result onMatch = match == null ? Result.NEUTRAL : match;
66          final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
67          return new LevelRangeFilter(actualMinLevel, actualMaxLevel, onMatch, onMismatch);
68      }
69      private final Level maxLevel;
70  
71      private final Level minLevel;
72  
73      private LevelRangeFilter(final Level minLevel, final Level maxLevel, final Result onMatch, final Result onMismatch) {
74          super(onMatch, onMismatch);
75          this.minLevel = minLevel;
76          this.maxLevel = maxLevel;
77      }
78  
79      private Result filter(final Level level) {
80          return level.isInRange(this.minLevel, this.maxLevel) ? onMatch : onMismatch;
81      }
82  
83      @Override
84      public Result filter(final LogEvent event) {
85          return filter(event.getLevel());
86      }
87  
88      @Override
89      public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
90              final Throwable t) {
91          return filter(level);
92      }
93  
94      @Override
95      public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
96              final Throwable t) {
97          return filter(level);
98      }
99  
100     @Override
101     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
102             final Object... params) {
103         return filter(level);
104     }
105 
106     public Level getMinLevel() {
107         return minLevel;
108     }
109 
110     @Override
111     public String toString() {
112         return minLevel.toString();
113     }
114 
115 }