001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.pattern; 018 019import java.io.PrintWriter; 020import java.io.StringWriter; 021import java.util.ArrayList; 022import java.util.Collections; 023import java.util.List; 024 025import org.apache.logging.log4j.core.LogEvent; 026import org.apache.logging.log4j.core.config.Configuration; 027import org.apache.logging.log4j.core.config.plugins.Plugin; 028import org.apache.logging.log4j.core.impl.ThrowableFormatOptions; 029import org.apache.logging.log4j.core.layout.PatternLayout; 030import org.apache.logging.log4j.core.util.StringBuilderWriter; 031import org.apache.logging.log4j.util.Strings; 032 033 034/** 035 * Outputs the Throwable portion of the LoggingEvent as a full stack trace 036 * unless this converter's option is 'short', where it just outputs the first line of the trace, or if 037 * the number of lines to print is explicitly specified. 038 */ 039@Plugin(name = "ThrowablePatternConverter", category = PatternConverter.CATEGORY) 040@ConverterKeys({ "ex", "throwable", "exception" }) 041public class ThrowablePatternConverter extends LogEventPatternConverter { 042 043 /** 044 * Lists {@link PatternFormatter}s for the suffix attribute. 045 */ 046 protected final List<PatternFormatter> formatters; 047 private String rawOption; 048 private final boolean subShortOption; 049 private final boolean nonStandardLineSeparator; 050 051 /** 052 * Options. 053 */ 054 protected final ThrowableFormatOptions options; 055 056 /** 057 * Constructor. 058 * @param name Name of converter. 059 * @param style CSS style for output. 060 * @param options options, may be null. 061 * @param config 062 */ 063 protected ThrowablePatternConverter(final String name, final String style, final String[] options, final Configuration config) { 064 super(name, style); 065 this.options = ThrowableFormatOptions.newInstance(options); 066 if (options != null && options.length > 0) { 067 rawOption = options[0]; 068 } 069 if (this.options.getSuffix() != null) { 070 final PatternParser parser = PatternLayout.createPatternParser(config); 071 final List<PatternFormatter> parsedSuffixFormatters = parser.parse(this.options.getSuffix()); 072 // filter out nested formatters that will handle throwable 073 boolean hasThrowableSuffixFormatter = false; 074 for (final PatternFormatter suffixFormatter : parsedSuffixFormatters) { 075 if (suffixFormatter.handlesThrowable()) { 076 hasThrowableSuffixFormatter = true; 077 } 078 } 079 if (!hasThrowableSuffixFormatter) { 080 this.formatters = parsedSuffixFormatters; 081 } else { 082 final List<PatternFormatter> suffixFormatters = new ArrayList<>(); 083 for (final PatternFormatter suffixFormatter : parsedSuffixFormatters) { 084 if (!suffixFormatter.handlesThrowable()) { 085 suffixFormatters.add(suffixFormatter); 086 } 087 } 088 this.formatters = suffixFormatters; 089 } 090 } else { 091 this.formatters = Collections.emptyList(); 092 } 093 subShortOption = ThrowableFormatOptions.MESSAGE.equalsIgnoreCase(rawOption) || 094 ThrowableFormatOptions.LOCALIZED_MESSAGE.equalsIgnoreCase(rawOption) || 095 ThrowableFormatOptions.FILE_NAME.equalsIgnoreCase(rawOption) || 096 ThrowableFormatOptions.LINE_NUMBER.equalsIgnoreCase(rawOption) || 097 ThrowableFormatOptions.METHOD_NAME.equalsIgnoreCase(rawOption) || 098 ThrowableFormatOptions.CLASS_NAME.equalsIgnoreCase(rawOption); 099 nonStandardLineSeparator = !Strings.LINE_SEPARATOR.equals(this.options.getSeparator()); 100 } 101 102 /** 103 * Gets an instance of the class. 104 * 105 * @param config 106 * @param options pattern options, may be null. If first element is "short", 107 * only the first line of the throwable will be formatted. 108 * @return instance of class. 109 */ 110 public static ThrowablePatternConverter newInstance(final Configuration config, final String[] options) { 111 return new ThrowablePatternConverter("Throwable", "throwable", options, config); 112 } 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 public void format(final LogEvent event, final StringBuilder buffer) { 119 final Throwable t = event.getThrown(); 120 121 if (subShortOption) { 122 formatSubShortOption(t, getSuffix(event), buffer); 123 } 124 else if (t != null && options.anyLines()) { 125 formatOption(t, getSuffix(event), buffer); 126 } 127 } 128 129 private void formatSubShortOption(final Throwable t, final String suffix, final StringBuilder buffer) { 130 StackTraceElement[] trace; 131 StackTraceElement throwingMethod = null; 132 int len; 133 134 if (t != null) { 135 trace = t.getStackTrace(); 136 if (trace !=null && trace.length > 0) { 137 throwingMethod = trace[0]; 138 } 139 } 140 141 if (t != null && throwingMethod != null) { 142 String toAppend = Strings.EMPTY; 143 144 if (ThrowableFormatOptions.CLASS_NAME.equalsIgnoreCase(rawOption)) { 145 toAppend = throwingMethod.getClassName(); 146 } 147 else if (ThrowableFormatOptions.METHOD_NAME.equalsIgnoreCase(rawOption)) { 148 toAppend = throwingMethod.getMethodName(); 149 } 150 else if (ThrowableFormatOptions.LINE_NUMBER.equalsIgnoreCase(rawOption)) { 151 toAppend = String.valueOf(throwingMethod.getLineNumber()); 152 } 153 else if (ThrowableFormatOptions.MESSAGE.equalsIgnoreCase(rawOption)) { 154 toAppend = t.getMessage(); 155 } 156 else if (ThrowableFormatOptions.LOCALIZED_MESSAGE.equalsIgnoreCase(rawOption)) { 157 toAppend = t.getLocalizedMessage(); 158 } 159 else if (ThrowableFormatOptions.FILE_NAME.equalsIgnoreCase(rawOption)) { 160 toAppend = throwingMethod.getFileName(); 161 } 162 163 len = buffer.length(); 164 if (len > 0 && !Character.isWhitespace(buffer.charAt(len - 1))) { 165 buffer.append(' '); 166 } 167 buffer.append(toAppend); 168 169 if (Strings.isNotBlank(suffix)) { 170 buffer.append(' '); 171 buffer.append(suffix); 172 } 173 } 174 } 175 176 private void formatOption(final Throwable throwable, final String suffix, final StringBuilder buffer) { 177 final int len = buffer.length(); 178 if (len > 0 && !Character.isWhitespace(buffer.charAt(len - 1))) { 179 buffer.append(' '); 180 } 181 if (!options.allLines() || nonStandardLineSeparator || Strings.isNotBlank(suffix)) { 182 final StringWriter w = new StringWriter(); 183 throwable.printStackTrace(new PrintWriter(w)); 184 185 final String[] array = w.toString().split(Strings.LINE_SEPARATOR); 186 final int limit = options.minLines(array.length) - 1; 187 final boolean suffixNotBlank = Strings.isNotBlank(suffix); 188 for (int i = 0; i <= limit; ++i) { 189 buffer.append(array[i]); 190 if (suffixNotBlank) { 191 buffer.append(' '); 192 buffer.append(suffix); 193 } 194 if (i < limit) { 195 buffer.append(options.getSeparator()); 196 } 197 } 198 } else { 199 throwable.printStackTrace(new PrintWriter(new StringBuilderWriter(buffer))); 200 } 201 } 202 203 /** 204 * This converter obviously handles throwables. 205 * 206 * @return true. 207 */ 208 @Override 209 public boolean handlesThrowable() { 210 return true; 211 } 212 213 protected String getSuffix(final LogEvent event) { 214 if (formatters.isEmpty()) { 215 return Strings.EMPTY; 216 } 217 //noinspection ForLoopReplaceableByForEach 218 final StringBuilder toAppendTo = new StringBuilder(); 219 for (int i = 0, size = formatters.size(); i < size; i++) { 220 formatters.get(i).format(event, toAppendTo); 221 } 222 return toAppendTo.toString(); 223 } 224 225 public ThrowableFormatOptions getOptions() { 226 return options; 227 } 228}