Java 在 printStackTrace() 上打印完整的调用堆栈?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1043378/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:38:08  来源:igfitidea点击:

Print full call stack on printStackTrace()?

javaloggingstack-trace

提问by akarnokd

I need to write small a log analyzer application to process some log files generated by a 3rd party closed source library (having custom logger inside) used in my project.

我需要编写一个小的日志分析器应用程序来处理由我的项目中使用的 3rd 方封闭源库(内部有自定义记录器)生成的一些日志文件。

In case of an exception entry in the log I need to collect aggregated information about the methods involved along the stack trace from the top to the actual place of the exception.

如果日志中出现异常条目,我需要收集有关从顶部到异常实际位置的堆栈跟踪中涉及的方法的汇总信息。

Unfortunately, by default Java printStackTrace() does not print every method in the call stack but up to a certain number and the rest is just referenced as 16 more....

不幸的是,默认情况下,Java printStackTrace() 不会打印调用堆栈中的每个方法,而是打印到某个数量,其余的只是作为16 more....

If I could catch the exception myself I would use the getStackTrace() and print it myself but the root cause is never included in the exception this library throws.

如果我可以自己捕获异常,我会使用 getStackTrace() 并自己打印它,但根本原因永远不会包含在这个库抛出的异常中。

Is there a way to ask Java to print the entire call stack in the stacktrace?

有没有办法让 Java 在堆栈跟踪中打印整个调用堆栈?

Apart from my situation do common logging frameworks have option for this?

除了我的情况外,常见的日志框架是否有这个选项?

Edit:The program runs on Sun's JVM with JDK 1.5.0_09. No option to change that.

编辑:该程序在带有 JDK 1.5.0_09 的 Sun JVM 上运行。没有办法改变这一点。

采纳答案by akf

here is an explanationof the 'caused by' and '... nmore' lines in the printed trace. see also the JavaDoc for printStackTrace. you might not have any work to do.

这是对打印轨迹中“由”和“... n更多”行的解释。另请参阅printStackTraceJavaDoc。你可能没有任何工作要做。

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.

请注意包含字符“...”的行的存在。这些行表示此异常的堆栈跟踪的其余部分与由此异常(“封闭”异常)引起的异常的堆栈跟踪底部的指示帧数相匹配。在从与捕获“原因异常”相同的方法抛出包装异常的常见情况下,这种简写可以大大减少输出的长度。

回答by A_M

Can't you do something with Thread.currentThread().getStackTrace()?

你不能做点什么Thread.currentThread().getStackTrace()吗?

Here's a real simple example which calls a method recursively 20 times and then dumps out the stack of the current thread.

这是一个真正简单的例子,它递归地调用一个方法 20 次,然后转储出当前线程的堆栈。

public class Test {
    public static void main(String[] args) {
        method();
    }

    static int x = 0;
    private static void method() {
        if(x>20) {
            StackTraceElement[] elements = Thread.currentThread().getStackTrace();

            for(int i=0; i<elements.length; i++) {
                System.out.println(elements[i]);
            }
        }
        else {
            x++;
            method();
        }
    }
}

回答by Manuel Selva

May be you can try to iterate over the array returned by :

也许您可以尝试遍历由以下返回的数组:

Thread.currentThread().getStackTrace();