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  import org.apache.logging.log4j.util.PerformanceSensitive;
26  
27  /**
28   * VariablesNotEmpty pattern converter.
29   */
30  @Plugin(name = "notEmpty", category = PatternConverter.CATEGORY)
31  @ConverterKeys({ "notEmpty", "varsNotEmpty", "variablesNotEmpty", })
32  @PerformanceSensitive("allocation")
33  public final class VariablesNotEmptyReplacementConverter extends LogEventPatternConverter {
34  
35      private final List<PatternFormatter> formatters;
36  
37      /**
38       * Constructs the converter.
39       *
40       * @param formatters
41       *            The PatternFormatters to generate the text to manipulate.
42       */
43      private VariablesNotEmptyReplacementConverter(final List<PatternFormatter> formatters) {
44          super("notEmpty", "notEmpty");
45          this.formatters = formatters;
46      }
47  
48      /**
49       * Gets an instance of the class.
50       *
51       * @param config
52       *            The current Configuration.
53       * @param options
54       *            pattern options, may be null.
55       * @return instance of class.
56       */
57      public static VariablesNotEmptyReplacementConverter newInstance(final Configuration config,
58              final String[] options) {
59          if (options.length != 1) {
60              LOGGER.error("Incorrect number of options on varsNotEmpty. Expected 1 received " + options.length);
61              return null;
62          }
63          if (options[0] == null) {
64              LOGGER.error("No pattern supplied on varsNotEmpty");
65              return null;
66          }
67          final PatternParser parser = PatternLayout.createPatternParser(config);
68          final List<PatternFormatter> formatters = parser.parse(options[0]);
69          return new VariablesNotEmptyReplacementConverter(formatters);
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public void format(final LogEvent event, final StringBuilder toAppendTo) {
77          final int start = toAppendTo.length();
78          boolean allVarsEmpty = true;
79          boolean hasVars = false;
80          for (int i = 0; i < formatters.size(); i++) {
81              final PatternFormatter formatter = formatters.get(i);
82              final int formatterStart = toAppendTo.length();
83              formatter.format(event, toAppendTo);
84              if (formatter.getConverter().isVariable()) {
85                  hasVars = true;
86                  allVarsEmpty = allVarsEmpty && (toAppendTo.length() == formatterStart);
87              }
88          }
89          if (!hasVars || allVarsEmpty) {
90              toAppendTo.setLength(start); // remove formatter results
91          }
92      }
93  }