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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.log4j.pattern;
20 import org.apache.log4j.spi.LoggingEvent;
21 import org.apache.log4j.spi.ThrowableInformation;
25 * Outputs the ThrowableInformation portion of the LoggingEvent.
26 * By default, outputs the full stack trace. %throwable{none}
27 * or %throwable{0} suppresses the stack trace. %throwable{short}
28 * or %throwable{1} outputs just the first line. %throwable{n}
29 * will output n lines for a positive integer or drop the last
30 * -n lines for a negative integer.
35 public class ThrowableInformationPatternConverter
36 extends LoggingEventPatternConverter {
39 * Maximum lines of stack trace to output.
41 private int maxLines = Integer.MAX_VALUE;
44 * Private constructor.
45 * @param options options, may be null.
47 private ThrowableInformationPatternConverter(
48 final String[] options) {
49 super("Throwable", "throwable");
51 if ((options != null) && (options.length > 0)) {
52 if("none".equals(options[0])) {
54 } else if("short".equals(options[0])) {
58 maxLines = Integer.parseInt(options[0]);
59 } catch(NumberFormatException ex) {
66 * Gets an instance of the class.
67 * @param options pattern options, may be null. If first element is "short",
68 * only the first line of the throwable will be formatted.
69 * @return instance of class.
71 public static ThrowableInformationPatternConverter newInstance(
72 final String[] options) {
73 return new ThrowableInformationPatternConverter(options);
79 public void format(final LoggingEvent event, final StringBuffer toAppendTo) {
81 ThrowableInformation information = event.getThrowableInformation();
83 if (information != null) {
84 String[] stringRep = information.getThrowableStrRep();
86 int length = stringRep.length;
89 } else if (length > maxLines) {
93 for (int i = 0; i < length; i++) {
94 String string = stringRep[i];
95 toAppendTo.append(string).append("\n");
102 * This converter obviously handles throwables.
105 public boolean handlesThrowable() {