Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以XML或JSON格式输出Java堆栈跟踪_Java_Xml_Stack Trace - Fatal编程技术网

以XML或JSON格式输出Java堆栈跟踪

以XML或JSON格式输出Java堆栈跟踪,java,xml,stack-trace,Java,Xml,Stack Trace,标准堆栈跟踪格式适用于人类。 如何以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; publi

标准堆栈跟踪格式适用于人类。 如何以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" : [ ]
}