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 org.apache.logging.log4j.core.LogEvent; 020import org.apache.logging.log4j.core.config.Configuration; 021import org.apache.logging.log4j.core.config.plugins.Plugin; 022import org.apache.logging.log4j.core.impl.ThrowableProxy; 023import org.apache.logging.log4j.util.Strings; 024 025/** 026 * Outputs the Throwable portion of the LoggingEvent as a full stack trace 027 * unless this converter's option is 'short', where it just outputs the first line of the trace, or if 028 * the number of lines to print is explicitly specified. 029 * <p> 030 * The extended stack trace will also include the location of where the class was loaded from and the 031 * version of the jar if available. 032 */ 033@Plugin(name = "RootThrowablePatternConverter", category = PatternConverter.CATEGORY) 034@ConverterKeys({ "rEx", "rThrowable", "rException" }) 035public final class RootThrowablePatternConverter extends ThrowablePatternConverter { 036 037 /** 038 * Private constructor. 039 * 040 * @param config 041 * @param options options, may be null. 042 */ 043 private RootThrowablePatternConverter(final Configuration config, final String[] options) { 044 super("RootThrowable", "throwable", options, config); 045 } 046 047 /** 048 * Gets an instance of the class. 049 * 050 * @param config 051 * @param options pattern options, may be null. If first element is "short", 052 * only the first line of the throwable will be formatted. 053 * @return instance of class. 054 */ 055 public static RootThrowablePatternConverter newInstance(final Configuration config, final String[] options) { 056 return new RootThrowablePatternConverter(config, options); 057 } 058 059 /** 060 * {@inheritDoc} 061 */ 062 @Override 063 public void format(final LogEvent event, final StringBuilder toAppendTo) { 064 final ThrowableProxy proxy = event.getThrownProxy(); 065 final Throwable throwable = event.getThrown(); 066 if (throwable != null && options.anyLines()) { 067 if (proxy == null) { 068 super.format(event, toAppendTo); 069 return; 070 } 071 final String trace = proxy.getCauseStackTraceAsString(options.getIgnorePackages(), options.getTextRenderer(), getSuffix(event), options.getSeparator()); 072 final int len = toAppendTo.length(); 073 if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) { 074 toAppendTo.append(' '); 075 } 076 if (!options.allLines() || !Strings.LINE_SEPARATOR.equals(options.getSeparator())) { 077 final StringBuilder sb = new StringBuilder(); 078 final String[] array = trace.split(Strings.LINE_SEPARATOR); 079 final int limit = options.minLines(array.length) - 1; 080 for (int i = 0; i <= limit; ++i) { 081 sb.append(array[i]); 082 if (i < limit) { 083 sb.append(options.getSeparator()); 084 } 085 } 086 toAppendTo.append(sb.toString()); 087 088 } else { 089 toAppendTo.append(trace); 090 } 091 } 092 } 093}