views:

2806

answers:

3

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.

+1  A: 

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();
    }
}

}

A_M
Quote: "If I could catch the exception myself I would use the getStackTrace() and print it myself "
kd304
So you are in no way in control of the code in anyway? You are simply looking at log files? Re-reading your question again, I guess not. Apologies! I'll leave the source code posted though in case anyone finds it useful.
A_M
Its a 3rd party library for some legacy operations. Cannot even JAD it without errors. Based on akf's answer I might have just misinterpreted the stacktrace printout format. I rarely have the task of analyzing a stacktrace programmatically.
kd304
A: 

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

Thread.currentThread().getStackTrace();
Manuel Selva
Quote: "If I could catch the exception myself I would use the getStackTrace() and print it myself "
kd304
The method getStackTrace() on Thread object should return the complete stack trace of the current thread, isn't it ?If the root exception was thrown in the same thread it should work ?May be i am missing something .. ?
Manuel Selva
There is an exception happening inside the library. It logs this exception stack trace into its log file then instantiates its own exception (call it LibException) containing the message of the root cause (LibException: FileNotFoundException ...) but does not assigns the root cause. Most probably the library is written for Java 1.2 where there was no constructor with root cause parameter and initCause() method is since 1.4.
kd304
+4  A: 

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.

akf
Good point. I might have misunderstood the ... notion as being a simple sortening form when the call stack is too large. I guess when there is an exception without root case thrown nested 100 frames deep I would get the that 100 lines in the printout?
kd304
i would hope so, but there is a warning in the JavaDoc for Throwable's getStackTrace(): "Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace." at the heart of getOurStackTrace() there are two native methods that control this behavior: getStackTraceDepth() and getStackTraceElement(int index)
akf
Thanks. Could you also reflect on the second question? Might need to analyze my own log4j output some day. It would be much simpler to have a full print and don't worry about jumping back and forth between the causes and the main exception.
kd304
you are looking for a way to print the stack disregarding the 'cause' Throwable?
akf
No, but hoped that some logging library - as it has access to the getStackTrace() - provides option to print it fully with cause if there is one.
kd304
in the link i put above, there is reference to Appache's Commons Lang library which provides an ExceptionsUtils class. in it there are helpers that print the trace 'upside-down' which easier to follow.
akf
Thank you. I guess I will just tailor some open source logging framework for this specific need.
kd304