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