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;
029import org.apache.logging.log4j.util.PerformanceSensitive;
030
031/**
032 * This filter returns the onMatch result if the marker in the LogEvent is the same as or has the
033 * configured marker as a parent.
034 */
035@Plugin(name = "MarkerFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
036@PerformanceSensitive("allocation")
037public final class MarkerFilter extends AbstractFilter {
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 Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
075            final Object p0) {
076        return filter(marker);
077    }
078
079    @Override
080    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
081            final Object p0, final Object p1) {
082        return filter(marker);
083    }
084
085    @Override
086    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
087            final Object p0, final Object p1, final Object p2) {
088        return filter(marker);
089    }
090
091    @Override
092    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
093            final Object p0, final Object p1, final Object p2, final Object p3) {
094        return filter(marker);
095    }
096
097    @Override
098    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
099            final Object p0, final Object p1, final Object p2, final Object p3,
100            final Object p4) {
101        return filter(marker);
102    }
103
104    @Override
105    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
106            final Object p0, final Object p1, final Object p2, final Object p3,
107            final Object p4, final Object p5) {
108        return filter(marker);
109    }
110
111    @Override
112    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
113            final Object p0, final Object p1, final Object p2, final Object p3,
114            final Object p4, final Object p5, final Object p6) {
115        return filter(marker);
116    }
117
118    @Override
119    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
120            final Object p0, final Object p1, final Object p2, final Object p3,
121            final Object p4, final Object p5, final Object p6,
122            final Object p7) {
123        return filter(marker);
124    }
125
126    @Override
127    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
128            final Object p0, final Object p1, final Object p2, final Object p3,
129            final Object p4, final Object p5, final Object p6,
130            final Object p7, final Object p8) {
131        return filter(marker);
132    }
133
134    @Override
135    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
136            final Object p0, final Object p1, final Object p2, final Object p3,
137            final Object p4, final Object p5, final Object p6,
138            final Object p7, final Object p8, final Object p9) {
139        return filter(marker);
140    }
141
142    @Override
143    public String toString() {
144        return name;
145    }
146
147    /**
148     * Create the MarkerFilter.
149     * @param marker The Marker name to match.
150     * @param match The action to take if a match occurs.
151     * @param mismatch The action to take if no match occurs.
152     * @return A MarkerFilter.
153     */
154    @PluginFactory
155    public static MarkerFilter createFilter(
156            @PluginAttribute("marker") final String marker,
157            @PluginAttribute("onMatch") final Result match,
158            @PluginAttribute("onMismatch") final Result mismatch) {
159
160        if (marker == null) {
161            LOGGER.error("A marker must be provided for MarkerFilter");
162            return null;
163        }
164        return new MarkerFilter(marker, match, mismatch);
165    }
166
167}