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       * Do not format the exception.
37       */
38      protected static final String NONE = "none";
39      /**
40       * Format the whole stack trace.
41       */
42      protected static final String FULL = "full";
43      /**
44       * Format only the first line of the throwable.
45       */
46      protected static final String SHORT = "short";
47      /**
48       * If "short", only first line of throwable report will be formatted.<br>
49       * If "full", the whole stack trace will be formatted.<br>
50       * If "numeric" the output will be limited to the specified number of lines.
51       */
52      protected final String option;
53  
54      /**
55       * The number of lines to write.
56       */
57      protected final int lines;
58  
59      /**
60       * Constructor.
61       * @param name Name of converter.
62       * @param style CSS style for output.
63       * @param options options, may be null.
64       */
65      protected ThrowablePatternConverter(final String name, final String style, final String[] options) {
66          super(name, style);
67          int count = Integer.MAX_VALUE;
68          if (options != null && options.length > 0) {
69              option = options[0];
70              if (option == null) {
71              } else if (option.equalsIgnoreCase(NONE)) {
72                  count = 0;
73              } else if (option.equalsIgnoreCase(SHORT)) {
74                  count = 2;
75              } else if (!option.equalsIgnoreCase(FULL)) {
76                  count = Integer.parseInt(option);
77              }
78  
79          } else {
80              option = null;
81          }
82          lines = count;
83      }
84  
85      /**
86       * Gets an instance of the class.
87       *
88       * @param options pattern options, may be null.  If first element is "short",
89       *                only the first line of the throwable will be formatted.
90       * @return instance of class.
91       */
92      public static ThrowablePatternConverter newInstance(final String[] options) {
93          return new ThrowablePatternConverter("Throwable", "throwable", options);
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public void format(final LogEvent event, final StringBuilder toAppendTo) {
101         final Throwable t = event.getThrown();
102 
103         if (t != null && lines > 0) {
104             final StringWriter w = new StringWriter();
105             t.printStackTrace(new PrintWriter(w));
106             final int len = toAppendTo.length();
107             if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) {
108                 toAppendTo.append(" ");
109             }
110             if (lines != Integer.MAX_VALUE) {
111                 final StringBuilder sb = new StringBuilder();
112                 final String[] array = w.toString().split("\n");
113                 final int limit = lines > array.length ? array.length : lines;
114                 for (int i = 0; i < limit; ++i) {
115                     sb.append(array[i]).append("\n");
116                 }
117                 toAppendTo.append(sb.toString());
118 
119             } else {
120                 toAppendTo.append(w.toString());
121             }
122         }
123     }
124 
125     /**
126      * This converter obviously handles throwables.
127      *
128      * @return true.
129      */
130     @Override
131     public boolean handlesThrowable() {
132         return true;
133     }
134 }