001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.filter;
018
019import org.apache.logging.log4j.Level;
020import org.apache.logging.log4j.Marker;
021import org.apache.logging.log4j.core.Filter;
022import org.apache.logging.log4j.core.LogEvent;
023import org.apache.logging.log4j.core.Logger;
024import org.apache.logging.log4j.core.config.Node;
025import org.apache.logging.log4j.core.config.plugins.Plugin;
026import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
027import org.apache.logging.log4j.core.config.plugins.PluginFactory;
028import org.apache.logging.log4j.message.Message;
029import org.apache.logging.log4j.util.PerformanceSensitive;
030
031/**
032 * This filter returns the {@code onMatch} result if the level in the {@code LogEvent} is in the range of the configured
033 * min and max levels, otherwise it returns {@code onMismatch} value . For example, if the filter is configured with
034 * {@link Level#ERROR} and {@link Level#INFO} and the LogEvent contains {@link Level#WARN} then the onMatch value will
035 * be returned since {@link Level#WARN WARN} events are in between {@link Level#ERROR ERROR} and {@link Level#INFO
036 * INFO}.
037 * <p>
038 * The default Levels are both {@link Level#ERROR ERROR}.
039 * </p>
040 */
041@Plugin(name = "LevelRangeFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
042@PerformanceSensitive("allocation")
043public final class LevelRangeFilter extends AbstractFilter {
044
045    /**
046     * Create a ThresholdFilter.
047     *
048     * @param minLevel
049     *            The minimum log Level.
050     * @param maxLevel
051     *            The maximum log Level.
052     * @param match
053     *            The action to take on a match.
054     * @param mismatch
055     *            The action to take on a mismatch.
056     * @return The created ThresholdFilter.
057     */
058    @PluginFactory
059    public static LevelRangeFilter createFilter(
060            // @formatter:off
061            @PluginAttribute("minLevel") final Level minLevel,
062            @PluginAttribute("maxLevel") final Level maxLevel,
063            @PluginAttribute("onMatch") final Result match,
064            @PluginAttribute("onMismatch") final Result mismatch) {
065            // @formatter:on
066        final Level actualMinLevel = minLevel == null ? Level.ERROR : minLevel;
067        final Level actualMaxLevel = maxLevel == null ? Level.ERROR : maxLevel;
068        final Result onMatch = match == null ? Result.NEUTRAL : match;
069        final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
070        return new LevelRangeFilter(actualMinLevel, actualMaxLevel, onMatch, onMismatch);
071    }
072    private final Level maxLevel;
073
074    private final Level minLevel;
075
076    private LevelRangeFilter(final Level minLevel, final Level maxLevel, final Result onMatch, final Result onMismatch) {
077        super(onMatch, onMismatch);
078        this.minLevel = minLevel;
079        this.maxLevel = maxLevel;
080    }
081
082    private Result filter(final Level level) {
083        return level.isInRange(this.minLevel, this.maxLevel) ? onMatch : onMismatch;
084    }
085
086    @Override
087    public Result filter(final LogEvent event) {
088        return filter(event.getLevel());
089    }
090
091    @Override
092    public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
093            final Throwable t) {
094        return filter(level);
095    }
096
097    @Override
098    public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
099            final Throwable t) {
100        return filter(level);
101    }
102
103    @Override
104    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
105            final Object... params) {
106        return filter(level);
107    }
108
109    @Override
110    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
111            final Object p0) {
112        return filter(level);
113    }
114
115    @Override
116    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
117            final Object p0, final Object p1) {
118        return filter(level);
119    }
120
121    @Override
122    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
123            final Object p0, final Object p1, final Object p2) {
124        return filter(level);
125    }
126
127    @Override
128    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
129            final Object p0, final Object p1, final Object p2, final Object p3) {
130        return filter(level);
131    }
132
133    @Override
134    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
135            final Object p0, final Object p1, final Object p2, final Object p3,
136            final Object p4) {
137        return filter(level);
138    }
139
140    @Override
141    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
142            final Object p0, final Object p1, final Object p2, final Object p3,
143            final Object p4, final Object p5) {
144        return filter(level);
145    }
146
147    @Override
148    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
149            final Object p0, final Object p1, final Object p2, final Object p3,
150            final Object p4, final Object p5, final Object p6) {
151        return filter(level);
152    }
153
154    @Override
155    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
156            final Object p0, final Object p1, final Object p2, final Object p3,
157            final Object p4, final Object p5, final Object p6,
158            final Object p7) {
159        return filter(level);
160    }
161
162    @Override
163    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
164            final Object p0, final Object p1, final Object p2, final Object p3,
165            final Object p4, final Object p5, final Object p6,
166            final Object p7, final Object p8) {
167        return filter(level);
168    }
169
170    @Override
171    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
172            final Object p0, final Object p1, final Object p2, final Object p3,
173            final Object p4, final Object p5, final Object p6,
174            final Object p7, final Object p8, final Object p9) {
175        return filter(level);
176    }
177
178    public Level getMinLevel() {
179        return minLevel;
180    }
181
182    @Override
183    public String toString() {
184        return minLevel.toString();
185    }
186
187}