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 final Level level; 042 043 private ThresholdFilter(final Level level, final Result onMatch, final Result onMismatch) { 044 super(onMatch, onMismatch); 045 this.level = level; 046 } 047 048 @Override 049 public Result filter(final Logger logger, final Level level, final Marker marker, final String msg, 050 final Object... params) { 051 return filter(level); 052 } 053 054 @Override 055 public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg, 056 final Throwable t) { 057 return filter(level); 058 } 059 060 @Override 061 public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg, 062 final Throwable t) { 063 return filter(level); 064 } 065 066 @Override 067 public Result filter(final LogEvent event) { 068 return filter(event.getLevel()); 069 } 070 071 private Result filter(final Level level) { 072 return level.isMoreSpecificThan(this.level) ? onMatch : onMismatch; 073 } 074 075 public Level getLevel() { 076 return level; 077 } 078 079 @Override 080 public String toString() { 081 return level.toString(); 082 } 083 084 /** 085 * Create a ThresholdFilter. 086 * @param level The log Level. 087 * @param match The action to take on a match. 088 * @param mismatch The action to take on a mismatch. 089 * @return The created ThresholdFilter. 090 */ 091 @PluginFactory 092 public static ThresholdFilter createFilter( 093 @PluginAttribute("level") final Level level, 094 @PluginAttribute("onMatch") final Result match, 095 @PluginAttribute("onMismatch") final Result mismatch) { 096 final Level actualLevel = level == null ? Level.ERROR : level; 097 final Result onMatch = match == null ? Result.NEUTRAL : match; 098 final Result onMismatch = mismatch == null ? Result.DENY : mismatch; 099 return new ThresholdFilter(actualLevel, onMatch, onMismatch); 100 } 101 102}