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 minimum log Level. 049 * @param maxLevel 050 * The maximum log Level. 051 * @param match 052 * The action to take on a match. 053 * @param mismatch 054 * The action to take on a mismatch. 055 * @return The created ThresholdFilter. 056 */ 057 @PluginFactory 058 public static LevelRangeFilter createFilter( 059 // @formatter:off 060 @PluginAttribute("minLevel") final Level minLevel, 061 @PluginAttribute("maxLevel") final Level maxLevel, 062 @PluginAttribute("onMatch") final Result match, 063 @PluginAttribute("onMismatch") final Result mismatch) { 064 // @formatter:on 065 final Level actualMinLevel = minLevel == null ? Level.ERROR : minLevel; 066 final Level actualMaxLevel = minLevel == null ? Level.ERROR : maxLevel; 067 final Result onMatch = match == null ? Result.NEUTRAL : match; 068 final Result onMismatch = mismatch == null ? Result.DENY : mismatch; 069 return new LevelRangeFilter(actualMinLevel, actualMaxLevel, onMatch, onMismatch); 070 } 071 private final Level maxLevel; 072 073 private final Level minLevel; 074 075 private LevelRangeFilter(final Level minLevel, final Level maxLevel, final Result onMatch, final Result onMismatch) { 076 super(onMatch, onMismatch); 077 this.minLevel = minLevel; 078 this.maxLevel = maxLevel; 079 } 080 081 private Result filter(final Level level) { 082 return level.isInRange(this.minLevel, this.maxLevel) ? onMatch : onMismatch; 083 } 084 085 @Override 086 public Result filter(final LogEvent event) { 087 return filter(event.getLevel()); 088 } 089 090 @Override 091 public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg, 092 final Throwable t) { 093 return filter(level); 094 } 095 096 @Override 097 public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg, 098 final Throwable t) { 099 return filter(level); 100 } 101 102 @Override 103 public Result filter(final Logger logger, final Level level, final Marker marker, final String msg, 104 final Object... params) { 105 return filter(level); 106 } 107 108 public Level getMinLevel() { 109 return minLevel; 110 } 111 112 @Override 113 public String toString() { 114 return minLevel.toString(); 115 } 116 117}