Java 如何将堆栈跟踪转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1149703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert a stack trace to a string?
提问by ripper234
What is the easiest way to convert the result of Throwable.getStackTrace()
to a string that depicts the stacktrace?
将 的结果转换为Throwable.getStackTrace()
描述堆栈跟踪的字符串的最简单方法是什么?
采纳答案by amar
One can use the following method to convert an Exception
stack trace to String
. This class is available in Apache commons-lang which is most common dependent library with many popular open sources
可以使用以下方法将Exception
堆栈跟踪转换为String
. 这个类在Apache commons-lang 中可用,它是许多流行的开源最常见的依赖库
org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(Throwable)
org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(Throwable)
回答by Brian Agnew
Use Throwable.printStackTrace(PrintWriter pw)
to send the stack trace to an appropriate writer.
使用Throwable.printStackTrace(PrintWriter pw)
堆栈跟踪发送到合适的作家。
import java.io.StringWriter;
import java.io.PrintWriter;
// ...
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String sStackTrace = sw.toString(); // stack trace as a string
System.out.println(sStackTrace);
回答by jqno
WARNING: Does not include cause (which is usually the useful bit!)
警告:不包括原因(这通常是有用的部分!)
public String stackTraceToString(Throwable e) {
StringBuilder sb = new StringBuilder();
for (StackTraceElement element : e.getStackTrace()) {
sb.append(element.toString());
sb.append("\n");
}
return sb.toString();
}
回答by D. Wroblewski
This should work:
这应该有效:
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
回答by Jarek Przygódzki
Printing stack trace to string
将堆栈跟踪打印到字符串
import java.io.PrintWriter;
import java.io.StringWriter;
public class StackTraceUtils {
public static String stackTraceToString(StackTraceElement[] stackTrace) {
StringWriter sw = new StringWriter();
printStackTrace(stackTrace, new PrintWriter(sw));
return sw.toString();
}
public static void printStackTrace(StackTraceElement[] stackTrace, PrintWriter pw) {
for(StackTraceElement stackTraceEl : stackTrace) {
pw.println(stackTraceEl);
}
}
}
It's useful when you want to print the current thread stack trace without creating instance of Throwable
- but note that creating new Throwable
and getting stack trace from there is actually faster and cheaper than calling Thread.getStackTrace
.
当您想要打印当前线程的堆栈跟踪,而无需创建实例它是有用的Throwable
-但请注意,创造新的Throwable
和得到的堆栈跟踪与实际速度更快,也比电话更便宜Thread.getStackTrace
。
回答by Vladas Dir?ys
For me the cleanest and easiest way was:
对我来说,最干净、最简单的方法是:
import java.util.Arrays;
Arrays.toString(e.getStackTrace());
回答by stumbav
Guava's Throwables
class
番石榴Throwables
类
If you have the actual Throwable
instance, Google Guavaprovides Throwables.getStackTraceAsString()
.
如果您有实际Throwable
实例,Google Guava 会提供Throwables.getStackTraceAsString()
.
Example:
例子:
String s = Throwables.getStackTraceAsString ( myException ) ;
回答by Vicky Kapadia
If you are developing for Android, a far easier way is to use this:
如果您正在为 Android 开发,一个更简单的方法是使用它:
import android.util.Log;
String stackTrace = Log.getStackTraceString(exception);
The format is the same as getStacktrace, for e.g.
格式与 getStacktrace 相同,例如
09-24 16:09:07.042: I/System.out(4844): java.lang.NullPointerException 09-24 16:09:07.042: I/System.out(4844): at com.temp.ttscancel.MainActivity.onCreate(MainActivity.java:43) 09-24 16:09:07.042: I/System.out(4844): at android.app.Activity.performCreate(Activity.java:5248) 09-24 16:09:07.043: I/System.out(4844): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 09-24 16:09:07.043: I/System.out(4844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162) 09-24 16:09:07.043: I/System.out(4844): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 09-24 16:09:07.043: I/System.out(4844): at android.app.ActivityThread.access0(ActivityThread.java:139) 09-24 16:09:07.043: I/System.out(4844): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 09-24 16:09:07.043: I/System.out(4844): at android.os.Handler.dispatchMessage(Handler.java:102) 09-24 16:09:07.043: I/System.out(4844): at android.os.Looper.loop(Looper.java:136) 09-24 16:09:07.044: I/System.out(4844): at android.app.ActivityThread.main(ActivityThread.java:5097) 09-24 16:09:07.044: I/System.out(4844): at java.lang.reflect.Method.invokeNative(Native Method) 09-24 16:09:07.044: I/System.out(4844): at java.lang.reflect.Method.invoke(Method.java:515) 09-24 16:09:07.044: I/System.out(4844): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 09-24 16:09:07.044: I/System.out(4844): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-24 16:09:07.042: I/System.out(4844): java.lang.NullPointerException 09-24 16:09:07.042: I/System.out(4844): at com.temp.ttscancel.MainActivity.onCreate(MainActivity.java:43) 09-24 16:09:07.042: I/System.out(4844): at android.app.Activity.performCreate(Activity.java:5248) 09-24 16:09:07.043: I/System.out(4844): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 09-24 16:09:07.043: I/System.out(4844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162) 09-24 16:09:07.043: I/System.out(4844): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 09-24 16:09:07.043: I/System.out(4844): at android.app.ActivityThread.access0(ActivityThread.java:139) 09-24 16:09:07.043: I/System.out(4844): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 09-24 16:09:07.043: I/System.out(4844): at android.os.Handler.dispatchMessage(Handler.java:102) 09-24 16:09:07.043: I/System.out(4844): at android.os.Looper.loop(Looper.java:136) 09-24 16:09:07.044: I/System.out(4844): at android.app.ActivityThread.main(ActivityThread.java:5097) 09-24 16:09:07.044: I/System.out(4844): at java.lang.reflect.Method.invokeNative(Native Method) 09-24 16:09:07.044: I/System.out(4844): at java.lang.reflect.Method.invoke(Method.java:515) 09-24 16:09:07.044: I/System.out(4844): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 09-24 16:09:07.044: I/System.out(4844): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
回答by rouble
Here is a version that is copy-pastable directly into code:
这是一个可直接复制粘贴到代码中的版本:
import java.io.StringWriter;
import java.io.PrintWriter;
//Two lines of code to get the exception into a StringWriter
StringWriter sw = new StringWriter();
new Throwable().printStackTrace(new PrintWriter(sw));
//And to actually print it
logger.info("Current stack trace is:\n" + sw.toString());
Or, in a catch block
或者,在 catch 块中
} catch (Throwable t) {
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
logger.info("Current stack trace is:\n" + sw.toString());
}
回答by dumonderic
The following code allows you to get the entire stackTrace with a String
format, without using APIs like log4J or even java.util.Logger
:
以下代码允许您使用某种String
格式获取整个 stackTrace ,而无需使用诸如 log4J 之类的 API,甚至java.util.Logger
:
catch (Exception e) {
StackTraceElement[] stack = e.getStackTrace();
String exception = "";
for (StackTraceElement s : stack) {
exception = exception + s.toString() + "\n\t\t";
}
System.out.println(exception);
// then you can send the exception string to a external file.
}