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 marker in the LogEvent is the same as or has the 032 * configured marker as a parent. 033 * 034 */ 035 @Plugin(name = "MarkerFilter", type = "Core", elementType = "filter", printObject = true) 036 public final class MarkerFilter extends AbstractFilter { 037 038 private final String name; 039 040 private MarkerFilter(String name, Result onMatch, Result onMismatch) { 041 super(onMatch, onMismatch); 042 this.name = name; 043 } 044 045 @Override 046 public Result filter(Logger logger, Level level, Marker marker, String msg, Object[] params) { 047 return filter(marker); 048 } 049 050 @Override 051 public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) { 052 return filter(marker); 053 } 054 055 @Override 056 public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) { 057 return filter(marker); 058 } 059 060 @Override 061 public Result filter(LogEvent event) { 062 return filter(event.getMarker()); 063 } 064 065 private Result filter(Marker marker) { 066 return marker != null && marker.isInstanceOf(name) ? onMatch : onMismatch; 067 } 068 069 @Override 070 public String toString() { 071 return name; 072 } 073 074 /** 075 * Create the MarkerFilter. 076 * @param marker The Marker name to match. 077 * @param match The action to take if a match occurs. 078 * @param mismatch The action to take if no match occurs. 079 * @return A MarkerFilter. 080 */ 081 @PluginFactory 082 public static MarkerFilter createFilter(@PluginAttr("marker") String marker, 083 @PluginAttr("onMatch") String match, 084 @PluginAttr("onMismatch") String mismatch) { 085 086 if (marker == null) { 087 LOGGER.error("A marker must be provided for MarkerFilter"); 088 return null; 089 } 090 Result onMatch = Result.toResult(match); 091 Result onMismatch = Result.toResult(mismatch); 092 return new MarkerFilter(marker, onMatch, onMismatch); 093 } 094 095 }