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