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