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.LogEvent;
022import org.apache.logging.log4j.core.Logger;
023import org.apache.logging.log4j.core.config.plugins.Plugin;
024import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
025import org.apache.logging.log4j.core.config.plugins.PluginFactory;
026import org.apache.logging.log4j.message.Message;
027
028/**
029 * This filter returns the onMatch result if the level in the LogEvent is the same or more specific
030 * than the configured level and the onMismatch value otherwise. For example, if the ThresholdFilter
031 * is configured with Level ERROR and the LogEvent contains Level DEBUG then the onMismatch value will
032 * be returned since ERROR events are more specific than DEBUG.
033 *
034 * The default Level is ERROR.
035 */
036@Plugin(name = "ThresholdFilter", category = "Core", elementType = "filter", printObject = true)
037public final class ThresholdFilter extends AbstractFilter {
038
039    private final Level level;
040
041    private ThresholdFilter(final Level level, final Result onMatch, final Result onMismatch) {
042        super(onMatch, onMismatch);
043        this.level = level;
044    }
045
046    @Override
047    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
048                         final Object... params) {
049        return filter(level);
050    }
051
052    @Override
053    public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
054                         final Throwable t) {
055        return filter(level);
056    }
057
058    @Override
059    public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
060                         final Throwable t) {
061        return filter(level);
062    }
063
064    @Override
065    public Result filter(final LogEvent event) {
066        return filter(event.getLevel());
067    }
068
069    private Result filter(final Level level) {
070        return level.isMoreSpecificThan(this.level) ? onMatch : onMismatch;
071    }
072
073    @Override
074    public String toString() {
075        return level.toString();
076    }
077
078    /**
079     * Create a ThresholdFilter.
080     * @param level The log Level.
081     * @param match The action to take on a match.
082     * @param mismatch The action to take on a mismatch.
083     * @return The created ThresholdFilter.
084     */
085    @PluginFactory
086    public static ThresholdFilter createFilter(
087            @PluginAttribute("level") final Level level,
088            @PluginAttribute("onMatch") final Result match,
089            @PluginAttribute("onMismatch") final Result mismatch) {
090        final Level actualLevel = level == null ? Level.ERROR : level;
091        final Result onMatch = match == null ? Result.NEUTRAL : match;
092        final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
093        return new ThresholdFilter(actualLevel, onMatch, onMismatch);
094    }
095
096}