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