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