如何在java中打印完整的堆栈跟踪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2970361/
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 to print the full stacktrace in java
提问by Enrique San Martín
I want to read the full stack trace of an exception that I capture.
我想读取我捕获的异常的完整堆栈跟踪。
For example:
例如:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.ibm.db2.jcc.DB2Driver'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1136)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at com.azurian.lce.usuarios.ConnectionManager.getConnection(ConnectionManager.java:65)
at com.azurian.lce.usuarios.db2.UsuarioDAOImpl.autenticar(UsuarioDAOImpl.java:101)
at com.azurian.lce.usuarios.UsuarioServiceImpl.autenticar(UsuarioServiceImpl.java:31)
at com.azurian.lce.web.admin.actions.LoginAction.execute(LoginAction.java:49)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: COM.ibm.db2.jcc.DB2Driver
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1130)
... 23 more
I want to read the "... 23 more" to see where the exception comes from.
我想阅读“... 23 more”以了解异常的来源。
采纳答案by BalusC
The answer is simple, those lines are already in the stacktrace :)
答案很简单,这些行已经在堆栈跟踪中了 :)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at com.azurian.lce.usuarios.ConnectionManager.getConnection(ConnectionManager.java:65)
at com.azurian.lce.usuarios.db2.UsuarioDAOImpl.autenticar(UsuarioDAOImpl.java:101)
at com.azurian.lce.usuarios.UsuarioServiceImpl.autenticar(UsuarioServiceImpl.java:31)
at com.azurian.lce.web.admin.actions.LoginAction.execute(LoginAction.java:49)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Basically, the following is happening in BasicDataSource#createDataSource()
:
基本上,以下发生在BasicDataSource#createDataSource()
:
try {
Class.forName(driverClassName); // Line 1130
} catch (ClassNotFoundException e) {
throw new SQLNestedException(e, "Cannot load JDBC driver class '" + driverClassName + "'"); // Line 1136
}
回答by mikej
BalusCis right. See here: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Throwable.html#printStackTrace()
BalusC是对的。见这里:http: //java.sun.com/j2se/1.5.0/docs/api/java/lang/Throwable.html#printStackTrace()
In particular:
特别是:
Note the presence of lines containing the characters "...". These lines indicate that the remainder of the stack trace for this exception matches the indicated number of frames from the bottom of the stack trace of the exception that was caused by this exception (the "enclosing" exception). This shorthand can greatly reduce the length of the output in the common case where a wrapped exception is thrown from same method as the "causative exception" is caught.
请注意包含字符“...”的行的存在。这些行表示此异常的堆栈跟踪的其余部分与由此异常(“封闭”异常)引起的异常的堆栈跟踪底部的指示帧数相匹配。在从与捕获“原因异常”相同的方法抛出包装异常的常见情况下,这种速记可以大大减少输出的长度。
What this means in your example is that:
这在您的示例中意味着:
BasicDataSource.java
line 1136 caught the ClassNotFoundException
thrown on line 1130 and reraised it as a SQLNestedException
. Hence the remainder of the stacktrace for the ClassNotFoundException
matches the SQLNestedException above and the stacktrace is printed in this more concise format.
BasicDataSource.java
第 1136 行接住了ClassNotFoundException
第 1130 行抛出的SQLNestedException
. 因此,ClassNotFoundException
堆栈跟踪的其余部分与上面的 SQLNestedException 匹配,堆栈跟踪以这种更简洁的格式打印。
回答by mdma
When the outer exception (SQLNestedException) wraps the inner exception (ClassNotFoundError) they are on the same thread, and so share a common base to their stack trace.
当外部异常 (SQLNestedException) 包装内部异常 (ClassNotFoundError) 时,它们位于同一线程上,因此它们的堆栈跟踪共享一个公共基础。
The (23 more...) shows where that common stack starts for the inner exception, which is also the place where the outer exception was thrown. So, whenever you see (XX more...), just look to the exception above to see the rest of the stack trace.
(23 more...) 显示了内部异常的公共堆栈开始的位置,这也是抛出外部异常的地方。因此,每当您看到(XX more...)时,只需查看上面的异常即可查看堆栈跟踪的其余部分。
If you want to programmatically print out the stacktrace without the ellipsis for common traces, then you can use Throwable.getStackTrace()and print out all the elements yourself.
如果您想以编程方式打印出不带省略号的堆栈跟踪,则可以使用Throwable.getStackTrace()并自己打印出所有元素。
回答by arcamax
Try this out. This logic loops through the main exception and all the causes of it, till there is no more cause (cause == null
) left to process. This way you can avoid the 23 more... message. I have not yet tested this yet, but i believe this should work out for you.
试试这个。该逻辑循环遍历主要异常及其所有原因,直到没有其他原因 ( cause == null
) 需要处理。这样您就可以避免 23 more... 消息。我还没有测试过这个,但我相信这应该适合你。
BTW - logWriter is a buffered writer. You may want to use System.out.print or any other logging API.
顺便说一句 - logWriter 是一个缓冲写入器。您可能希望使用 System.out.print 或任何其他日志记录 API。
public static void debugError(final String message, final Throwable th) {
final String logMessage = "[ERROR] - " + message;
try {
logWriter.write(logMessage);
logWriter.newLine();
// dump exception stack if specified
if (null != th) {
final StackTraceElement[] traces = th.getStackTrace();
if (null != traces && traces.length > 0) {
logWriter.write(th.getClass() + ": " + th.getMessage());
logWriter.newLine();
for (final StackTraceElement trace : traces) {
logWriter.write(" at " + trace.getClassName() + '.' + trace.getMethodName() + '(' + trace.getFileName() + ':' + trace.getLineNumber() + ')');
logWriter.newLine();
}
}
Throwable cause = th.getCause();
while (null != cause) {
final StackTraceElement[] causeTraces = cause.getStackTrace();
if (null != causeTraces && causeTraces.length > 0) {
logWriter.write("Caused By:");
logWriter.newLine();
logWriter.write(cause.getClass() + ": " + cause.getMessage());
logWriter.newLine();
for (final StackTraceElement causeTrace : causeTraces) {
logWriter.write(" at " + causeTrace.getClassName() + '.' + causeTrace.getMethodName() + '(' + causeTrace.getFileName() + ':' + causeTrace.getLineNumber() + ')');
logWriter.newLine();
}
}
// fetch next cause
cause = cause.getCause();
}
}
} catch (final IOException ex) {
System.err.println(logMessage);
if (null != th) {
th.printStackTrace();
}
}
}