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