View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.pattern;
18  
19  import java.util.regex.Pattern;
20  
21  import org.apache.logging.log4j.Logger;
22  import org.apache.logging.log4j.core.Core;
23  import org.apache.logging.log4j.core.config.plugins.Plugin;
24  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
25  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
26  import org.apache.logging.log4j.status.StatusLogger;
27  
28  /**
29   * Replace tokens in the LogEvent message.
30   */
31  @Plugin(name = "replace", category = Core.CATEGORY_NAME, printObject = true)
32  public final class RegexReplacement {
33  
34      private static final Logger LOGGER = StatusLogger.getLogger();
35  
36      private final Pattern pattern;
37  
38      private final String substitution;
39  
40      /**
41       * Private constructor.
42       *
43       * @param pattern The Pattern.
44       * @param substitution The substitution String.
45       */
46      private RegexReplacement(final Pattern pattern, final String substitution) {
47          this.pattern = pattern;
48          this.substitution = substitution;
49      }
50  
51      /**
52       * Perform the replacement.
53       * @param msg The String to match against.
54       * @return the replacement String.
55       */
56      public String format(final String msg) {
57          return pattern.matcher(msg).replaceAll(substitution);
58      }
59  
60      @Override
61      public String toString() {
62          return "replace(regex=" + pattern.pattern() + ", replacement=" + substitution + ')';
63      }
64  
65      /**
66       * Create a RegexReplacement.
67       * @param regex The regular expression to locate.
68       * @param replacement The replacement value.
69       * @return A RegexReplacement.
70       */
71      @PluginFactory
72      public static RegexReplacement createRegexReplacement(
73              @PluginAttribute("regex") final Pattern regex,
74              @PluginAttribute("replacement") final String replacement) {
75          if (regex == null) {
76              LOGGER.error("A regular expression is required for replacement");
77              return null;
78          }
79          if (replacement == null) {
80              LOGGER.error("A replacement string is required to perform replacement");
81          }
82          // FIXME: should we use Matcher.quoteReplacement() here?
83          return new RegexReplacement(regex, replacement);
84      }
85  
86  }