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;
029
030/**
031 * This filter returns the onMatch result if the level in the LogEvent is in the
032 * range of the configured min and max levels, otherwise it returns onMismatch
033 * value . For example, if the filter is configured with Level ERROR and Level
034 * INFO and the LogEvent contains Level WARN then the onMatch value will be
035 * returned since WARN events are in between ERROR and INFO.
036 *
037 * The default Levels are both ERROR.
038 */
039@Plugin(name = "LevelRangeFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
040public final class LevelRangeFilter extends AbstractFilter {
041
042    private static final long serialVersionUID = 1L;
043
044    /**
045     * Create a ThresholdFilter.
046     * 
047     * @param minLevel
048     *            The log Level.
049     * @param match
050     *            The action to take on a match.
051     * @param mismatch
052     *            The action to take on a mismatch.
053     * @return The created ThresholdFilter.
054     */
055    @PluginFactory
056    public static LevelRangeFilter createFilter(
057            // @formatter:off
058            @PluginAttribute("minLevel") final Level minLevel,
059            @PluginAttribute("maxLevel") final Level maxLevel,
060            @PluginAttribute("onMatch") final Result match, 
061            @PluginAttribute("onMismatch") final Result mismatch) {
062            // @formatter:on
063        final Level actualMinLevel = minLevel == null ? Level.ERROR : minLevel;
064        final Level actualMaxLevel = minLevel == null ? Level.ERROR : maxLevel;
065        final Result onMatch = match == null ? Result.NEUTRAL : match;
066        final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
067        return new LevelRangeFilter(actualMinLevel, actualMaxLevel, onMatch, onMismatch);
068    }
069    private final Level maxLevel;
070
071    private final Level minLevel;
072
073    private LevelRangeFilter(final Level minLevel, final Level maxLevel, final Result onMatch, final Result onMismatch) {
074        super(onMatch, onMismatch);
075        this.minLevel = minLevel;
076        this.maxLevel = maxLevel;
077    }
078
079    private Result filter(final Level level) {
080        return level.isInRange(this.minLevel, this.maxLevel) ? onMatch : onMismatch;
081    }
082
083    @Override
084    public Result filter(final LogEvent event) {
085        return filter(event.getLevel());
086    }
087
088    @Override
089    public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
090            final Throwable t) {
091        return filter(level);
092    }
093
094    @Override
095    public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
096            final Throwable t) {
097        return filter(level);
098    }
099
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}