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 */ 017 package org.apache.logging.log4j.core.filter; 018 019 import java.util.Locale; 020 021 import org.apache.logging.log4j.Level; 022 import org.apache.logging.log4j.Marker; 023 import org.apache.logging.log4j.core.LogEvent; 024 import org.apache.logging.log4j.core.Logger; 025 import org.apache.logging.log4j.core.config.plugins.Plugin; 026 import org.apache.logging.log4j.core.config.plugins.PluginAttr; 027 import org.apache.logging.log4j.core.config.plugins.PluginFactory; 028 import 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", type = "Core", elementType = "filter", printObject = true) 039 public final class ThresholdFilter extends AbstractFilter { 040 041 private final Level level; 042 043 private ThresholdFilter(Level level, Result onMatch, Result onMismatch) { 044 super(onMatch, onMismatch); 045 this.level = level; 046 } 047 048 @Override 049 public Result filter(Logger logger, Level level, Marker marker, String msg, Object[] params) { 050 return filter(level); 051 } 052 053 @Override 054 public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) { 055 return filter(level); 056 } 057 058 @Override 059 public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) { 060 return filter(level); 061 } 062 063 @Override 064 public Result filter(LogEvent event) { 065 return filter(event.getLevel()); 066 } 067 068 private Result filter(Level level) { 069 return level.isAtLeastAsSpecificAs(this.level) ? onMatch : onMismatch; 070 } 071 072 @Override 073 public String toString() { 074 return level.toString(); 075 } 076 077 /** 078 * Create a ThresholdFilter. 079 * @param levelName The log Level. 080 * @param match The action to take on a match. 081 * @param mismatch The action to take on a mismatch. 082 * @return The created ThresholdFilter. 083 */ 084 @PluginFactory 085 public static ThresholdFilter createFilter(@PluginAttr("level") String levelName, 086 @PluginAttr("onMatch") String match, 087 @PluginAttr("onMismatch") String mismatch) { 088 Level level = Level.toLevel(levelName, Level.ERROR); 089 Result onMatch = Result.toResult(match, Result.NEUTRAL); 090 Result onMismatch = Result.toResult(mismatch, Result.DENY); 091 return new ThresholdFilter(level, onMatch, onMismatch); 092 } 093 094 }