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.LogEvent;
022import org.apache.logging.log4j.core.Logger;
023import org.apache.logging.log4j.core.config.plugins.Plugin;
024import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
025import org.apache.logging.log4j.core.config.plugins.PluginFactory;
026import 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", category = "Core", elementType = "filter", printObject = true)
034public 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(
084            @PluginAttribute("marker") final String marker,
085            @PluginAttribute("onMatch") final Result match,
086            @PluginAttribute("onMismatch") final Result mismatch) {
087
088        if (marker == null) {
089            LOGGER.error("A marker must be provided for MarkerFilter");
090            return null;
091        }
092        return new MarkerFilter(marker, match, mismatch);
093    }
094
095}