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