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 java.io.PrintWriter;
20  import java.io.StringWriter;
21  
22  import org.apache.logging.log4j.core.LogEvent;
23  import org.apache.logging.log4j.core.config.plugins.Plugin;
24  import org.apache.logging.log4j.core.helpers.Constants;
25  import org.apache.logging.log4j.core.impl.ThrowableFormatOptions;
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      private String rawOption;
38  
39      /**
40       * The number of lines to write.
41       */
42      protected final ThrowableFormatOptions options;
43  
44      /**
45       * Constructor.
46       * @param name Name of converter.
47       * @param style CSS style for output.
48       * @param options options, may be null.
49       */
50      protected ThrowablePatternConverter(final String name, final String style, final String[] options) {
51          super(name, style);
52          this.options = ThrowableFormatOptions.newInstance(options);
53          if (options != null && options.length > 0) {
54              rawOption = options[0];
55          }
56      }
57  
58      /**
59       * Gets an instance of the class.
60       *
61       * @param options pattern options, may be null.  If first element is "short",
62       *                only the first line of the throwable will be formatted.
63       * @return instance of class.
64       */
65      public static ThrowablePatternConverter newInstance(final String[] options) {
66          return new ThrowablePatternConverter("Throwable", "throwable", options);
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public void format(final LogEvent event, final StringBuilder buffer) {
74          final Throwable t = event.getThrown();
75  
76          if (isSubShortOption()) {
77              formatSubShortOption(t, buffer);
78          }
79          else if (t != null && options.anyLines()) {
80              formatOption(t, buffer);
81          }
82      }
83  
84      private boolean isSubShortOption() {
85          return ThrowableFormatOptions.MESSAGE.equalsIgnoreCase(rawOption) ||
86                  ThrowableFormatOptions.LOCALIZED_MESSAGE.equalsIgnoreCase(rawOption) ||
87                  ThrowableFormatOptions.FILE_NAME.equalsIgnoreCase(rawOption) ||
88                  ThrowableFormatOptions.LINE_NUMBER.equalsIgnoreCase(rawOption) ||
89                  ThrowableFormatOptions.METHOD_NAME.equalsIgnoreCase(rawOption) ||
90                  ThrowableFormatOptions.CLASS_NAME.equalsIgnoreCase(rawOption);
91      }
92  
93      private void formatSubShortOption(final Throwable t, final StringBuilder buffer) {
94          StackTraceElement[] trace;
95          StackTraceElement throwingMethod = null;
96          int len;
97  
98          if (t != null) {
99              trace = t.getStackTrace();
100             if (trace !=null && trace.length > 0) {
101                 throwingMethod = trace[0];
102             }
103         }
104 
105         if (t != null && throwingMethod != null) {
106             String toAppend = "";
107 
108             if (ThrowableFormatOptions.CLASS_NAME.equalsIgnoreCase(rawOption)) {
109                 toAppend = throwingMethod.getClassName();
110             }
111             else if (ThrowableFormatOptions.METHOD_NAME.equalsIgnoreCase(rawOption)) {
112                 toAppend = throwingMethod.getMethodName();
113             }
114             else if (ThrowableFormatOptions.LINE_NUMBER.equalsIgnoreCase(rawOption)) {
115                 toAppend = String.valueOf(throwingMethod.getLineNumber());
116             }
117             else if (ThrowableFormatOptions.MESSAGE.equalsIgnoreCase(rawOption)) {
118                 toAppend = t.getMessage();
119             }
120             else if (ThrowableFormatOptions.LOCALIZED_MESSAGE.equalsIgnoreCase(rawOption)) {
121                 toAppend = t.getLocalizedMessage();
122             }
123             else if (ThrowableFormatOptions.FILE_NAME.equalsIgnoreCase(rawOption)) {
124                 toAppend = throwingMethod.getFileName();
125             }
126 
127             len = buffer.length();
128             if (len > 0 && !Character.isWhitespace(buffer.charAt(len - 1))) {
129                 buffer.append(" ");
130             }
131             buffer.append(toAppend);
132         }
133     }
134 
135     private void formatOption(final Throwable throwable, final StringBuilder buffer) {
136         final StringWriter w = new StringWriter();
137 
138         throwable.printStackTrace(new PrintWriter(w));
139         final int len = buffer.length();
140         if (len > 0 && !Character.isWhitespace(buffer.charAt(len - 1))) {
141             buffer.append(' ');
142         }
143         if (!options.allLines() || !Constants.LINE_SEP.equals(options.getSeparator())) {
144             final StringBuilder sb = new StringBuilder();
145             final String[] array = w.toString().split(Constants.LINE_SEP);
146             final int limit = options.minLines(array.length) - 1;
147             for (int i = 0; i <= limit; ++i) {
148                 sb.append(array[i]);
149                 if (i < limit) {
150                     sb.append(options.getSeparator());
151                 }
152             }
153             buffer.append(sb.toString());
154 
155         } else {
156             buffer.append(w.toString());
157         }
158     }
159 
160     /**
161      * This converter obviously handles throwables.
162      *
163      * @return true.
164      */
165     @Override
166     public boolean handlesThrowable() {
167         return true;
168     }
169 }