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.pattern;
018
019import java.util.regex.Pattern;
020
021import org.apache.logging.log4j.Logger;
022import org.apache.logging.log4j.core.Core;
023import org.apache.logging.log4j.core.config.plugins.Plugin;
024import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
025import org.apache.logging.log4j.core.config.plugins.PluginFactory;
026import org.apache.logging.log4j.status.StatusLogger;
027
028/**
029 * Replace tokens in the LogEvent message.
030 */
031@Plugin(name = "replace", category = Core.CATEGORY_NAME, printObject = true)
032public final class RegexReplacement {
033
034    private static final Logger LOGGER = StatusLogger.getLogger();
035
036    private final Pattern pattern;
037
038    private final String substitution;
039
040    /**
041     * Private constructor.
042     *
043     * @param pattern The Pattern.
044     * @param substitution The substitution String.
045     */
046    private RegexReplacement(final Pattern pattern, final String substitution) {
047        this.pattern = pattern;
048        this.substitution = substitution;
049    }
050
051    /**
052     * Perform the replacement.
053     * @param msg The String to match against.
054     * @return the replacement String.
055     */
056    public String format(final String msg) {
057        return pattern.matcher(msg).replaceAll(substitution);
058    }
059
060    @Override
061    public String toString() {
062        return "replace(regex=" + pattern.pattern() + ", replacement=" + substitution + ')';
063    }
064
065    /**
066     * Create a RegexReplacement.
067     * @param regex The regular expression to locate.
068     * @param replacement The replacement value.
069     * @return A RegexReplacement.
070     */
071    @PluginFactory
072    public static RegexReplacement createRegexReplacement(
073            @PluginAttribute("regex") final Pattern regex,
074            @PluginAttribute("replacement") final String replacement) {
075        if (regex == null) {
076            LOGGER.error("A regular expression is required for replacement");
077            return null;
078        }
079        if (replacement == null) {
080            LOGGER.error("A replacement string is required to perform replacement");
081        }
082        // FIXME: should we use Matcher.quoteReplacement() here?
083        return new RegexReplacement(regex, replacement);
084    }
085
086}