0
標準堆棧跟蹤格式適用於人類。 如何以XML/JSON等結構化格式輸出Java堆棧跟蹤以供機器使用?以XML或JSON格式輸出Java堆棧跟蹤
標準堆棧跟蹤格式適用於人類。 如何以XML/JSON等結構化格式輸出Java堆棧跟蹤以供機器使用?以XML或JSON格式輸出Java堆棧跟蹤
你可以做
package stack43542547;
import java.io.StringWriter;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class HowToConvertStackTraceToJson {
@Test
public void convertStackTraceToJson() throws Exception {
try {
throw new NullPointerException();
} catch (Exception e) {
System.out.println(asJson(e));
}
}
private String asJson(Object obj) throws Exception {
StringWriter w = new StringWriter();
new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true).writeValue(w, obj);
String result = w.toString();
return result; }
}
給人
{
"cause" : null,
"stackTrace" : [ {
"methodName" : "convertStackTraceToJson",
"fileName" : "HowToConvertStackTraceToJson.java",
"lineNumber" : 12,
"className" : "stack43542547.HowToConvertStackTraceToJson",
"nativeMethod" : false
}, {
"methodName" : "invoke0",
"fileName" : "NativeMethodAccessorImpl.java",
"lineNumber" : -2,
"className" : "sun.reflect.NativeMethodAccessorImpl",
"nativeMethod" : true
}, {
"methodName" : "invoke",
"fileName" : "NativeMethodAccessorImpl.java",
"lineNumber" : 62,
"className" : "sun.reflect.NativeMethodAccessorImpl",
"nativeMethod" : false
}, {
"methodName" : "invoke",
"fileName" : "DelegatingMethodAccessorImpl.java",
"lineNumber" : 43,
"className" : "sun.reflect.DelegatingMethodAccessorImpl",
"nativeMethod" : false
}, {
"methodName" : "invoke",
"fileName" : "Method.java",
"lineNumber" : 483,
"className" : "java.lang.reflect.Method",
"nativeMethod" : false
}, {
"methodName" : "runReflectiveCall",
"fileName" : "FrameworkMethod.java",
"lineNumber" : 50,
"className" : "org.junit.runners.model.FrameworkMethod$1",
"nativeMethod" : false
}, {
"methodName" : "run",
"fileName" : "ReflectiveCallable.java",
"lineNumber" : 12,
"className" : "org.junit.internal.runners.model.ReflectiveCallable",
"nativeMethod" : false
}, {
"methodName" : "invokeExplosively",
"fileName" : "FrameworkMethod.java",
"lineNumber" : 47,
"className" : "org.junit.runners.model.FrameworkMethod",
"nativeMethod" : false
}, {
"methodName" : "evaluate",
"fileName" : "InvokeMethod.java",
"lineNumber" : 17,
"className" : "org.junit.internal.runners.statements.InvokeMethod",
"nativeMethod" : false
}, {
"methodName" : "runLeaf",
"fileName" : "ParentRunner.java",
"lineNumber" : 325,
"className" : "org.junit.runners.ParentRunner",
"nativeMethod" : false
}, {
"methodName" : "runChild",
"fileName" : "BlockJUnit4ClassRunner.java",
"lineNumber" : 78,
"className" : "org.junit.runners.BlockJUnit4ClassRunner",
"nativeMethod" : false
}, {
"methodName" : "runChild",
"fileName" : "BlockJUnit4ClassRunner.java",
"lineNumber" : 57,
"className" : "org.junit.runners.BlockJUnit4ClassRunner",
"nativeMethod" : false
}, {
"methodName" : "run",
"fileName" : "ParentRunner.java",
"lineNumber" : 290,
"className" : "org.junit.runners.ParentRunner$3",
"nativeMethod" : false
}, {
"methodName" : "schedule",
"fileName" : "ParentRunner.java",
"lineNumber" : 71,
"className" : "org.junit.runners.ParentRunner$1",
"nativeMethod" : false
}, {
"methodName" : "runChildren",
"fileName" : "ParentRunner.java",
"lineNumber" : 288,
"className" : "org.junit.runners.ParentRunner",
"nativeMethod" : false
}, {
"methodName" : "access$000",
"fileName" : "ParentRunner.java",
"lineNumber" : 58,
"className" : "org.junit.runners.ParentRunner",
"nativeMethod" : false
}, {
"methodName" : "evaluate",
"fileName" : "ParentRunner.java",
"lineNumber" : 268,
"className" : "org.junit.runners.ParentRunner$2",
"nativeMethod" : false
}, {
"methodName" : "run",
"fileName" : "ParentRunner.java",
"lineNumber" : 363,
"className" : "org.junit.runners.ParentRunner",
"nativeMethod" : false
}, {
"methodName" : "run",
"fileName" : "JUnit4TestReference.java",
"lineNumber" : 86,
"className" : "org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference",
"nativeMethod" : false
}, {
"methodName" : "run",
"fileName" : "TestExecution.java",
"lineNumber" : 38,
"className" : "org.eclipse.jdt.internal.junit.runner.TestExecution",
"nativeMethod" : false
}, {
"methodName" : "runTests",
"fileName" : "RemoteTestRunner.java",
"lineNumber" : 459,
"className" : "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner",
"nativeMethod" : false
}, {
"methodName" : "runTests",
"fileName" : "RemoteTestRunner.java",
"lineNumber" : 678,
"className" : "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner",
"nativeMethod" : false
}, {
"methodName" : "run",
"fileName" : "RemoteTestRunner.java",
"lineNumber" : 382,
"className" : "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner",
"nativeMethod" : false
}, {
"methodName" : "main",
"fileName" : "RemoteTestRunner.java",
"lineNumber" : 192,
"className" : "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner",
"nativeMethod" : false
} ],
"localizedMessage" : null,
"message" : null,
"suppressed" : [ ]
}
你在哪裏要產生的信息?捕獲異常或例如在日誌處理程序中? –
我不確定。這有什麼不同嗎?如果我有堆棧跟蹤對象,我不應該能夠以不同的方式打印它嗎? – m33lky