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 org.apache.logging.log4j.core.LogEvent;
20  import org.apache.logging.log4j.core.config.Configuration;
21  import org.apache.logging.log4j.core.config.plugins.Plugin;
22  import org.apache.logging.log4j.core.layout.PatternLayout;
23  
24  import java.util.List;
25  
26  /**
27   * Style pattern converter. Adds ANSI color styling to the result of the enclosed pattern.
28   */
29  @Plugin(name = "style", type = "Converter")
30  @ConverterKeys({"style" })
31  public final class StyleConverter extends LogEventPatternConverter {
32  
33      private final List<PatternFormatter> formatters;
34  
35      private final String style;
36  
37      /**
38       * Construct the converter.
39       * @param formatters The PatternFormatters to generate the text to manipulate.
40       * @param styling The styling that should encapsulate the pattern.
41       */
42      private StyleConverter(List<PatternFormatter> formatters, String styling) {
43          super("style", "style");
44          this.formatters = formatters;
45          this.style = styling;
46      }
47  
48      /**
49       * Gets an instance of the class.
50       *
51       * @param config The current Configuration.
52       * @param options pattern options, may be null.  If first element is "short",
53       *                only the first line of the throwable will be formatted.
54       * @return instance of class.
55       */
56      public static StyleConverter newInstance(Configuration config, final String[] options) {
57          if (options.length < 1) {
58              LOGGER.error("Incorrect number of options on style. Expected at least 1, received " + options.length);
59              return null;
60          }
61          if (options[0] == null) {
62              LOGGER.error("No pattern supplied on style");
63              return null;
64          }
65          if (options[1] == null) {
66              LOGGER.error("No style attributes provided");
67              return null;
68          }
69  
70          PatternParser parser = PatternLayout.createPatternParser(config);
71          List<PatternFormatter> formatters = parser.parse(options[0]);
72          String style = AnsiEscape.createSequence(options[1].split(","));
73          return new StyleConverter(formatters, style);
74      }
75  
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public void format(final LogEvent event, final StringBuilder toAppendTo) {
82          StringBuilder buf = new StringBuilder();
83          for (PatternFormatter formatter : formatters) {
84              formatter.format(event, buf);
85          }
86  
87          if (buf.length() > 0) {
88              toAppendTo.append(style).append(buf.toString()).append(AnsiEscape.getDefaultStyle());
89          }
90      }
91  }