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