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      /**
34       * Gets an instance of the class.
35       *
36       * @param config
37       *            The current Configuration.
38       * @param options
39       *            pattern options, may be null.
40       * @return instance of class.
41       */
42      public static VariablesNotEmptyReplacementConverter newInstance(final Configuration config,
43              final String[] options) {
44          if (options.length != 1) {
45              LOGGER.error("Incorrect number of options on varsNotEmpty. Expected 1 received " + options.length);
46              return null;
47          }
48          if (options[0] == null) {
49              LOGGER.error("No pattern supplied on varsNotEmpty");
50              return null;
51          }
52          final PatternParser parser = PatternLayout.createPatternParser(config);
53          final List<PatternFormatter> formatters = parser.parse(options[0]);
54          return new VariablesNotEmptyReplacementConverter(formatters);
55      }
56  
57      private final List<PatternFormatter> formatters;
58  
59      /**
60       * Construct the converter.
61       * 
62       * @param formatters
63       *            The PatternFormatters to generate the text to manipulate.
64       */
65      private VariablesNotEmptyReplacementConverter(final List<PatternFormatter> formatters) {
66          super("notEmpty", "notEmpty");
67          this.formatters = 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              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  }