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