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.List;
20  
21  import org.apache.logging.log4j.core.LogEvent;
22  import org.apache.logging.log4j.core.config.Configuration;
23  import org.apache.logging.log4j.core.config.plugins.Plugin;
24  import org.apache.logging.log4j.core.layout.PatternLayout;
25  
26  /**
27   * Equals pattern converter.
28   */
29  @Plugin(name = "equals", category = PatternConverter.CATEGORY)
30  @ConverterKeys({ "equals" })
31  public final class EqualsReplacementConverter extends LogEventPatternConverter {
32  
33      /**
34       * Gets an instance of the class.
35       *
36       * @param config  The current Configuration.
37       * @param options pattern options, an array of three elements: pattern, testString, and substitution.
38       * @return instance of class.
39       */
40      public static EqualsReplacementConverter newInstance(final Configuration config, final String[] options) {
41          if (options.length != 3) {
42              LOGGER.error("Incorrect number of options on equals. Expected 3 received " + options.length);
43              return null;
44          }
45          if (options[0] == null) {
46              LOGGER.error("No pattern supplied on equals");
47              return null;
48          }
49          if (options[1] == null) {
50              LOGGER.error("No test string supplied on equals");
51              return null;
52          }
53          if (options[2] == null) {
54              LOGGER.error("No substitution supplied on equals");
55              return null;
56          }
57          final String p = options[1];
58          final PatternParser parser = PatternLayout.createPatternParser(config);
59          final List<PatternFormatter> formatters = parser.parse(options[0]);
60          return new EqualsReplacementConverter(formatters, p, options[2], parser);
61      }
62  
63      private final List<PatternFormatter> formatters;
64  
65      private final String substitution;
66  
67      private final String testString;
68  
69      private final PatternParser parser;
70  
71      /**
72       * Construct the converter.
73       *
74       * @param formatters   The PatternFormatters to generate the text to manipulate.
75       * @param testString   The test string.
76       * @param substitution The substitution string.
77       * @param parser       The PatternParser.
78       */
79      private EqualsReplacementConverter(final List<PatternFormatter> formatters, final String testString,
80                                         final String substitution, final PatternParser parser) {
81          super("equals", "equals");
82          this.testString = testString;
83          this.substitution = substitution;
84          this.formatters = formatters;
85          this.parser = parser;
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public void format(final LogEvent event, final StringBuilder toAppendTo) {
93          final StringBuilder buf = new StringBuilder();
94          for (final PatternFormatter formatter : formatters) {
95              formatter.format(event, buf);
96          }
97          final String string = buf.toString();
98          toAppendTo.append(testString.equals(string) ? parseSubstitution(event) : string);
99      }
100 
101     /**
102      * Returns the parsed substitution text.
103      *
104      * @param event the current log event
105      * @return the parsed substitution text
106      */
107     String parseSubstitution(final LogEvent event) {
108         final StringBuilder substitutionBuffer = new StringBuilder();
109         // check if substitution needs to be parsed
110         if (substitution.contains("%")) {
111             // parse substitution pattern
112             final List<PatternFormatter> substitutionFormatters = parser.parse(substitution);
113             for (final PatternFormatter formatter : substitutionFormatters) {
114                 formatter.format(event, substitutionBuffer);
115             }
116         } else {
117             substitutionBuffer.append(substitution);
118         }
119         return substitutionBuffer.toString();
120     }
121 }