Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print full call stack on printStackTrace()?

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.

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....

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.

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

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.

like image 292
akarnokd Avatar asked Jun 25 '09 11:06

akarnokd


People also ask

How do I print a full stack trace?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.

What does the printStackTrace () method return?

Return Value: This method do not returns anything. Below programs illustrate the printStackTrace(PrintStream s) method of Java.

What is printStackTrace () in Java?

The printStackTrace() method in Java is a tool used to handle exceptions and errors. It is a method of Java's throwable class which prints the throwable along with other details like the line number and class name where the exception occurred. printStackTrace() is very useful in diagnosing exceptions.


2 Answers

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

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.

like image 147
akf Avatar answered Sep 22 '22 10:09

akf


Can't you do something with 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.

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();         }     } } 
like image 43
A_M Avatar answered Sep 23 '22 10:09

A_M