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.plugins.Plugin;
21  import org.apache.logging.log4j.core.helpers.Constants;
22  import org.apache.logging.log4j.core.impl.ThrowableFormatOptions;
23  
24  import java.io.PrintWriter;
25  import java.io.StringWriter;
26  
27  
28  /**
29   * Outputs the Throwable portion of the LoggingEvent as a full stacktrace
30   * unless this converter's option is 'short', where it just outputs the first line of the trace, or if
31   * the number of lines to print is explicitly specified.
32   */
33  @Plugin(name = "ThrowablePatternConverter", category = "Converter")
34  @ConverterKeys({"ex", "throwable", "exception" })
35  public class ThrowablePatternConverter extends LogEventPatternConverter {
36  
37      /**
38       * The number of lines to write.
39       */
40      protected final ThrowableFormatOptions options;
41  
42      /**
43       * Constructor.
44       * @param name Name of converter.
45       * @param style CSS style for output.
46       * @param options options, may be null.
47       */
48      protected ThrowablePatternConverter(final String name, final String style, final String[] options) {
49          super(name, style);
50          this.options = ThrowableFormatOptions.newInstance(options);
51      }
52  
53      /**
54       * Gets an instance of the class.
55       *
56       * @param options pattern options, may be null.  If first element is "short",
57       *                only the first line of the throwable will be formatted.
58       * @return instance of class.
59       */
60      public static ThrowablePatternConverter newInstance(final String[] options) {
61          return new ThrowablePatternConverter("Throwable", "throwable", options);
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public void format(final LogEvent event, final StringBuilder toAppendTo) {
69          final Throwable t = event.getThrown();
70  
71          if (t != null && options.anyLines()) {
72              final StringWriter w = new StringWriter();
73              t.printStackTrace(new PrintWriter(w));
74              final int len = toAppendTo.length();
75              if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) {
76                  toAppendTo.append(" ");
77              }
78              if (!options.allLines() || !Constants.LINE_SEP.equals(options.getSeparator())) {
79                  final StringBuilder sb = new StringBuilder();
80                  final String[] array = w.toString().split(Constants.LINE_SEP);
81                  final int limit = options.minLines(array.length) - 1;
82                  for (int i = 0; i <= limit; ++i) {
83                      sb.append(array[i]);
84                      if (i < limit) {
85                          sb.append(options.getSeparator());
86                      }
87                  }
88                  toAppendTo.append(sb.toString());
89  
90              } else {
91                  toAppendTo.append(w.toString());
92              }
93          }
94      }
95  
96      /**
97       * This converter obviously handles throwables.
98       *
99       * @return true.
100      */
101     @Override
102     public boolean handlesThrowable() {
103         return true;
104     }
105 }