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.PluginBuilderAttribute;
027import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
028import org.apache.logging.log4j.message.Message;
029import org.apache.logging.log4j.message.ParameterizedMessage;
030import org.apache.logging.log4j.util.PerformanceSensitive;
031
032/**
033 * This filter returns the onMatch result if the logging level in the event matches the specified logging level
034 * exactly.
035 */
036@Plugin(name = "StringMatchFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
037@PerformanceSensitive("allocation")
038public final class StringMatchFilter extends AbstractFilter {
039
040    public static final String ATTR_MATCH = "match";
041    private final String text;
042
043    private StringMatchFilter(final String text, final Result onMatch, final Result onMismatch) {
044        super(onMatch, onMismatch);
045        this.text = text;
046    }
047
048    @Override
049    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
050                         final Object... params) {
051        return filter(logger.getMessageFactory().newMessage(msg, params).getFormattedMessage());
052    }
053
054    @Override
055    public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
056                         final Throwable t) {
057        return filter(logger.getMessageFactory().newMessage(msg).getFormattedMessage());
058    }
059
060    @Override
061    public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
062                         final Throwable t) {
063        return filter(msg.getFormattedMessage());
064    }
065
066    @Override
067    public Result filter(final LogEvent event) {
068        return filter(event.getMessage().getFormattedMessage());
069    }
070
071    private Result filter(final String msg) {
072        return msg.contains(this.text) ? onMatch : onMismatch;
073    }
074
075    @Override
076    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
077            final Object p0) {
078        return filter(logger.getMessageFactory().newMessage(msg, p0).getFormattedMessage());
079    }
080
081    @Override
082    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
083            final Object p0, final Object p1) {
084        return filter(logger.getMessageFactory().newMessage(msg, p0, p1).getFormattedMessage());
085    }
086
087    @Override
088    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
089            final Object p0, final Object p1, final Object p2) {
090        return filter(logger.getMessageFactory().newMessage(msg, p0, p1, p2).getFormattedMessage());
091    }
092
093    @Override
094    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
095            final Object p0, final Object p1, final Object p2, final Object p3) {
096        return filter(logger.getMessageFactory().newMessage(msg, p0, p1, p2, p3).getFormattedMessage());
097    }
098
099    @Override
100    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
101            final Object p0, final Object p1, final Object p2, final Object p3,
102            final Object p4) {
103        return filter(logger.getMessageFactory().newMessage(msg, p0, p1, p2, p3, p4).getFormattedMessage());
104    }
105
106    @Override
107    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
108            final Object p0, final Object p1, final Object p2, final Object p3,
109            final Object p4, final Object p5) {
110        return filter(logger.getMessageFactory().newMessage(msg, p0, p1, p2, p3, p4, p5).getFormattedMessage());
111    }
112
113    @Override
114    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
115            final Object p0, final Object p1, final Object p2, final Object p3,
116            final Object p4, final Object p5, final Object p6) {
117        return filter(logger.getMessageFactory().newMessage(msg, p0, p1, p2, p3, p4, p5, p6).getFormattedMessage());
118    }
119
120    @Override
121    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
122            final Object p0, final Object p1, final Object p2, final Object p3,
123            final Object p4, final Object p5, final Object p6,
124            final Object p7) {
125        return filter(logger.getMessageFactory().newMessage(msg, p0, p1, p2, p3, p4, p5, p6, p7).getFormattedMessage());
126    }
127
128    @Override
129    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
130            final Object p0, final Object p1, final Object p2, final Object p3,
131            final Object p4, final Object p5, final Object p6,
132            final Object p7, final Object p8) {
133        return filter(logger.getMessageFactory().newMessage(msg, p0, p1, p2, p3, p4, p5, p6, p7, p8)
134                .getFormattedMessage());
135    }
136
137    @Override
138    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
139            final Object p0, final Object p1, final Object p2, final Object p3,
140            final Object p4, final Object p5, final Object p6,
141            final Object p7, final Object p8, final Object p9) {
142        return filter(logger.getMessageFactory().newMessage(msg, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9)
143                .getFormattedMessage());
144    }
145
146    @Override
147    public String toString() {
148        return text;
149    }
150
151    @PluginBuilderFactory
152    public static StringMatchFilter.Builder newBuilder() {
153        return new StringMatchFilter.Builder();
154    }
155
156    public static class Builder extends AbstractFilterBuilder<StringMatchFilter.Builder> implements org.apache.logging.log4j.core.util.Builder<StringMatchFilter> {
157        @PluginBuilderAttribute
158        private String text = "";
159
160        /**
161         * Sets the logging level to use.
162         * @param level the logging level to use.
163         * @return this
164         */
165        public StringMatchFilter.Builder setMatchString(final String text) {
166            this.text = text;
167            return this;
168        }
169
170        @Override
171        public StringMatchFilter build() {
172            return new StringMatchFilter(this.text, this.getOnMatch(), this.getOnMismatch());
173        }
174    }
175
176}