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  
22  import java.io.PrintWriter;
23  import java.io.StringWriter;
24  
25  
26  /**
27   * Outputs the Throwable portion of the LoggingEvent as a full stacktrace
28   * unless this converter's option is 'short', where it just outputs the first line of the trace, or if
29   * the number of lines to print is explicitly specified.
30   */
31  @Plugin(name = "ThrowablePatternConverter", type = "Converter")
32  @ConverterKeys({"ex", "throwable", "exception" })
33  public class ThrowablePatternConverter extends LogEventPatternConverter {
34  
35      /**
36       * Format the whole stack trace.
37       */
38      protected static final String FULL = "full";
39      /**
40       * Format only the first line of the throwable.
41       */
42      protected static final String SHORT = "short";
43      /**
44       * If "short", only first line of throwable report will be formatted.<br>
45       * If "full", the whole stack trace will be formatted.<br>
46       * If "numeric" the output will be limited to the specified number of lines.
47       */
48      protected final String option;
49  
50      /**
51       * The number of lines to write.
52       */
53      protected final int lines;
54  
55      /**
56       * Constructor.
57       * @param name Name of converter.
58       * @param style CSS style for output.
59       * @param options options, may be null.
60       */
61      protected ThrowablePatternConverter(String name, String style, final String[] options) {
62          super(name, style);
63          int count = 0;
64          if ((options != null) && (options.length > 0)) {
65              option = options[0];
66              if (option == null) {
67              } else if (option.equalsIgnoreCase(SHORT)) {
68                  count = 2;
69              } else if (!option.equalsIgnoreCase(FULL)) {
70                  count = Integer.parseInt(option);
71              }
72  
73          } else {
74              option = null;
75          }
76          lines = count;
77      }
78  
79      /**
80       * Gets an instance of the class.
81       *
82       * @param options pattern options, may be null.  If first element is "short",
83       *                only the first line of the throwable will be formatted.
84       * @return instance of class.
85       */
86      public static ThrowablePatternConverter newInstance(final String[] options) {
87          return new ThrowablePatternConverter("Throwable", "throwable", options);
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public void format(final LogEvent event, final StringBuilder toAppendTo) {
95          Throwable t = event.getThrown();
96  
97          if (t != null) {
98              StringWriter w = new StringWriter();
99              t.printStackTrace(new PrintWriter(w));
100             int len = toAppendTo.length();
101             if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) {
102                 toAppendTo.append(" ");
103             }
104             if (lines > 0) {
105                 StringBuilder sb = new StringBuilder();
106                 String[] array = w.toString().split("\n");
107                 for (int i = 0; i < lines; ++i) {
108                     sb.append(array[i]).append("\n");
109                 }
110                 toAppendTo.append(sb.toString());
111 
112             } else {
113                 toAppendTo.append(w.toString());
114             }
115         }
116     }
117 
118     /**
119      * This converter obviously handles throwables.
120      *
121      * @return true.
122      */
123     @Override
124     public boolean handlesThrowable() {
125         return true;
126     }
127 }