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  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Scanner;
26  
27  /**
28   * Outputs the Throwable portion of the LoggingEvent as a full stacktrace
29   * unless this converter's option is 'short', where it just outputs the first line of the trace, or if
30   * the number of lines to print is explicitly specified.
31   * <p>
32   * The extended stack trace will also include the location of where the class was loaded from and the
33   * version of the jar if available.
34   */
35  @Plugin(name = "ExtendedThrowablePatternConverter", type = "Converter")
36  @ConverterKeys({"xEx", "xThrowable", "xException" })
37  public final class ExtendedThrowablePatternConverter extends ThrowablePatternConverter {
38  
39      private static final String FILTERS = "filters(";
40  
41      private final List<String> packages;
42  
43      /**
44       * Private constructor.
45       *
46       * @param options options, may be null.
47       */
48      private ExtendedThrowablePatternConverter(final String[] options) {        
49          super("ExtendedThrowable", "throwable", options);
50          List<String> tempPackages = null;
51          if (options != null && options.length > 1) {
52              if (options[1].startsWith(FILTERS) && options[1].endsWith(")")) {
53                  String filterStr = options[1].substring(FILTERS.length(), options[1].length() - 1);
54                  String[] array = filterStr.split(",");
55                  if (array.length > 0) {
56                      tempPackages = new ArrayList<String>(array.length);
57                      for (String token : array) {
58                          tempPackages.add(token.trim());
59                      }
60                  }
61              }
62          }
63          packages = tempPackages;
64      }
65  
66      /**
67       * Gets an instance of the class.
68       *
69       * @param options pattern options, may be null.  If first element is "short",
70       *                only the first line of the throwable will be formatted.
71       * @return instance of class.
72       */
73      public static ExtendedThrowablePatternConverter newInstance(final String[] options) {
74          String type = null;
75          String[] array = options;
76          if (options != null && options.length == 1 && options[0].length() > 0) {
77              String[] opts = options[0].split(",", 2);
78              String first = opts[0].trim();
79              String filter;
80              Scanner scanner = new Scanner(first);
81              if (first.equalsIgnoreCase(FULL) || first.equalsIgnoreCase(SHORT) || scanner.hasNextInt()) {
82                  type = first;
83                  filter = opts[1].trim();
84              } else {
85                  filter = options[0].trim();
86              }
87              array = new String[] {type, filter};
88          }
89  
90          return new ExtendedThrowablePatternConverter(array);
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public void format(final LogEvent event, final StringBuilder toAppendTo) {
98          Throwable throwable = event.getThrown();
99          if (throwable != null) {
100             if (!(throwable instanceof ThrowableProxy)) {
101                 super.format(event, toAppendTo);
102                 return;
103             }
104             ThrowableProxy t = (ThrowableProxy) throwable;
105             String trace = t.getExtendedStackTrace(packages);
106             int len = toAppendTo.length();
107             if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) {
108                 toAppendTo.append(" ");
109             }
110             if (lines > 0) {
111                 StringBuilder sb = new StringBuilder();
112                 String[] array = trace.split("\n");
113                 for (int i = 0; i < lines; ++i) {
114                     sb.append(array[i]).append("\n");
115                 }
116                 toAppendTo.append(sb.toString());
117 
118             } else {
119                 toAppendTo.append(trace);
120             }
121         }
122     }
123 }