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.impl.ThrowableProxy;
22  
23  /**
24   * Outputs the Throwable portion of the LoggingEvent as a full stacktrace
25   * unless this converter's option is 'short', where it just outputs the first line of the trace, or if
26   * the number of lines to print is explicitly specified.
27   * <p>
28   * The extended stack trace will also include the location of where the class was loaded from and the
29   * version of the jar if available.
30   */
31  @Plugin(name = "ExtendedThrowablePatternConverter", type = "Converter")
32  @ConverterKeys({"xEx", "xThrowable", "xException" })
33  public final class ExtendedThrowablePatternConverter extends ThrowablePatternConverter {
34      /**
35       * Private constructor.
36       *
37       * @param options options, may be null.
38       */
39      private ExtendedThrowablePatternConverter(final String[] options) {
40          super("ExtendedThrowable", "throwable", options);
41      }
42  
43      /**
44       * Gets an instance of the class.
45       *
46       * @param options pattern options, may be null.  If first element is "short",
47       *                only the first line of the throwable will be formatted.
48       * @return instance of class.
49       */
50      public static ExtendedThrowablePatternConverter newInstance(
51          final String[] options) {
52          return new ExtendedThrowablePatternConverter(options);
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      public void format(final LogEvent event, final StringBuilder toAppendTo) {
59          Throwable throwable = event.getThrown();
60          if (throwable != null) {
61              if (!(throwable instanceof ThrowableProxy)) {
62                  super.format(event, toAppendTo);
63                  return;
64              }
65              ThrowableProxy t = (ThrowableProxy) throwable;
66              String trace = t.getExtendedStackTrace();
67              int len = toAppendTo.length();
68              if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) {
69                  toAppendTo.append(" ");
70              }
71              if (lines > 0) {
72                  StringBuilder sb = new StringBuilder();
73                  String[] array = trace.split("\n");
74                  for (int i = 0; i < lines; ++i) {
75                      sb.append(array[i]).append("\n");
76                  }
77                  toAppendTo.append(sb.toString());
78  
79              } else {
80                  toAppendTo.append(trace);
81              }
82          }
83      }
84  }