ch.qos.logback.classic.pattern.ThrowableProxyConverter Maven / Gradle / Ivy
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2011, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.classic.pattern;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.StackTraceElementProxy;
import ch.qos.logback.classic.spi.ThrowableProxyUtil;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.boolex.EvaluationException;
import ch.qos.logback.core.boolex.EventEvaluator;
import ch.qos.logback.core.status.ErrorStatus;
/**
* Add a stack trace in case the event contains a Throwable.
*
* @author Ceki Gülcü
*/
public class ThrowableProxyConverter extends ThrowableHandlingConverter {
int lengthOption;
List> evaluatorList = null;
int errorCount = 0;
@SuppressWarnings("unchecked")
public void start() {
String lengthStr = getFirstOption();
if (lengthStr == null) {
lengthOption = Integer.MAX_VALUE;
} else {
lengthStr = lengthStr.toLowerCase();
if ("full".equals(lengthStr)) {
lengthOption = Integer.MAX_VALUE;
} else if ("short".equals(lengthStr)) {
lengthOption = 2;
} else {
try {
// we add one because, printing starts at offset 1
lengthOption = Integer.parseInt(lengthStr) + 1;
} catch (NumberFormatException nfe) {
addError("Could not parser [" + lengthStr + " as an integer");
lengthOption = Integer.MAX_VALUE;
}
}
}
final List optionList = getOptionList();
if (optionList != null && optionList.size() > 1) {
final int optionListSize = optionList.size();
for (int i = 1; i < optionListSize; i++) {
String evaluatorStr = (String) optionList.get(i);
Context context = getContext();
Map evaluatorMap = (Map) context.getObject(CoreConstants.EVALUATOR_MAP);
EventEvaluator ee = (EventEvaluator) evaluatorMap
.get(evaluatorStr);
addEvaluator(ee);
}
}
super.start();
}
private void addEvaluator(EventEvaluator ee) {
if (evaluatorList == null) {
evaluatorList = new ArrayList>();
}
evaluatorList.add(ee);
}
public void stop() {
evaluatorList = null;
super.stop();
}
protected void extraData(StringBuilder builder, StackTraceElementProxy step) {
// nop
}
public String convert(ILoggingEvent event) {
IThrowableProxy tp = event.getThrowableProxy();
if (tp == null) {
return CoreConstants.EMPTY_STRING;
}
// an evaluator match will cause stack printing to be skipped
if (evaluatorList != null) {
boolean printStack = true;
for (int i = 0; i < evaluatorList.size(); i++) {
EventEvaluator ee = evaluatorList.get(i);
try {
if (ee.evaluate(event)) {
printStack = false;
break;
}
} catch (EvaluationException eex) {
errorCount++;
if (errorCount < CoreConstants.MAX_ERROR_COUNT) {
addError("Exception thrown for evaluator named [" + ee.getName()
+ "]", eex);
} else if (errorCount == CoreConstants.MAX_ERROR_COUNT) {
ErrorStatus errorStatus = new ErrorStatus(
"Exception thrown for evaluator named [" + ee.getName() + "].",
this, eex);
errorStatus.add(new ErrorStatus(
"This was the last warning about this evaluator's errors."
+ "We don't want the StatusManager to get flooded.", this));
addStatus(errorStatus);
}
}
}
if (!printStack) {
return CoreConstants.EMPTY_STRING;
}
}
return throwableProxyToString(tp);
}
protected String throwableProxyToString(IThrowableProxy tp) {
StringBuilder buf = new StringBuilder(32);
IThrowableProxy currentThrowable = tp;
while (currentThrowable != null) {
subjoinThrowableProxy(buf, currentThrowable);
currentThrowable = currentThrowable.getCause();
}
return buf.toString();
}
void subjoinThrowableProxy(StringBuilder buf, IThrowableProxy tp) {
ThrowableProxyUtil.subjoinFirstLine(buf, tp);
buf.append(CoreConstants.LINE_SEPARATOR);
StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
int commonFrames = tp.getCommonFrames();
boolean unrestrictedPrinting = lengthOption > stepArray.length;
int maxIndex = (unrestrictedPrinting) ? stepArray.length : lengthOption;
if (commonFrames > 0 && unrestrictedPrinting) {
maxIndex -= commonFrames;
}
for (int i = 0; i < maxIndex; i++) {
String string = stepArray[i].toString();
buf.append(CoreConstants.TAB);
buf.append(string);
extraData(buf, stepArray[i]); // allow other data to be added
buf.append(CoreConstants.LINE_SEPARATOR);
}
if (commonFrames > 0 && unrestrictedPrinting) {
buf.append("\t... ").append(tp.getCommonFrames()).append(
" common frames omitted").append(CoreConstants.LINE_SEPARATOR);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy