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.config.plugins.Plugin;
23  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
24  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
25  import org.apache.logging.log4j.status.StatusLogger;
26  
27  /**
28   * Replace tokens in the LogEvent message.
29   */
30  @Plugin(name = "replace", category = "Core", printObject = true)
31  public final class RegexReplacement {
32  
33      private static final Logger LOGGER = StatusLogger.getLogger();
34  
35      private final Pattern pattern;
36  
37      private final String substitution;
38  
39      /**
40       * Private constructor.
41       *
42       * @param pattern The Pattern.
43       * @param substitution The substitution String.
44       */
45      private RegexReplacement(final Pattern pattern, final String substitution) {
46          this.pattern = pattern;
47          this.substitution = substitution;
48      }
49  
50      /**
51       * Perform the replacement.
52       * @param msg The String to match against.
53       * @return the replacement String.
54       */
55      public String format(final String msg) {
56          return pattern.matcher(msg).replaceAll(substitution);
57      }
58  
59      @Override
60      public String toString() {
61          return "replace(regex=" + pattern.pattern() + ", replacement=" + substitution + ")";
62      }
63  
64      /**
65       * Create a RegexReplacement.
66       * @param regex The regular expression to locate.
67       * @param replacement The replacement value.
68       * @return A RegexReplacement.
69       */
70      @PluginFactory
71      public static RegexReplacement createRegexReplacement(
72              @PluginAttribute("regex") final String regex,
73              @PluginAttribute("replacement") final String replacement) {
74          if (regex == null) {
75              LOGGER.error("A regular expression is required for replacement");
76              return null;
77          }
78          if (replacement == null) {
79              LOGGER.error("A replacement string is required to perform replacement");
80          }
81          final Pattern p = Pattern.compile(regex);
82          return new RegexReplacement(p, replacement);
83      }
84  
85  }