发生异常时显示完整堆栈跟踪

时间:2011-07-14 09:35:09

标签: java exception tomcat logging stack-trace

  

可能重复:
  howto increase lines of java stack trace dump?

我正在为tomcat编写Java应用程序。每当抛出异常时,我都会得到异常的名称和堆栈跟踪,如果它是一个很长的跟踪,我得到 ... X more

如何配置tomcat以显示完整的堆栈跟踪?

谢谢你!

1 个答案:

答案 0 :(得分:3)

这不是Tomcat的错,它是Throwable.printStackTraceAsCause(PrintStream s, StackTraceElement[] causedTrace)方法实现堆栈跟踪打印的方式。

 /**
     * Print our stack trace as a cause for the specified stack trace.
     */
    private void printStackTraceAsCause(PrintStream s,
                                        StackTraceElement[] causedTrace)
    {
        // assert Thread.holdsLock(s);

        // Compute number of frames in common between this and caused
        StackTraceElement[] trace = getOurStackTrace();
        int m = trace.length-1, n = causedTrace.length-1;
        while (m >= 0 && n >=0 && trace[m].equals(causedTrace[n])) {
            m--; n--;
        }
        int framesInCommon = trace.length - 1 - m;

        s.println("Caused by: " + this);
        for (int i=0; i <= m; i++)
            s.println("\tat " + trace[i]);
        if (framesInCommon != 0)
            s.println("\t... " + framesInCommon + " more");

        // Recurse if we have a cause
        Throwable ourCause = getCause();
        if (ourCause != null)
            ourCause.printStackTraceAsCause(s, trace);
    }

如果需要,可以先在日志文件(或response.getOutputStream())中打印堆栈跟踪,方法是首先显示捕获元素的消息,然后显示StackTraceElement的数组元素(可以通过调用Throwable.getStackTrace()方法找到。