Is there any tool that can extract a list of stack traces appearing in the log file and probably count unique ones?
EDIT: I would preffer something that is not GUI-based and be run on the background and give some kind of report back. I have quite many logs gathered from several environments and just would like to get quick overview.
You can use Thread. currentThread(). getStackTrace() . That returns an array of StackTraceElement s that represent the current stack trace of a program.
You can obtain a stack trace from a thread – by calling the getStackTrace method on that Thread instance. This invocation returns an array of StackTraceElement, from which details about stack frames of the thread can be extracted.
To read this stack trace, start at the top with the Exception's type - ArithmeticException and message The denominator must not be zero . This gives an idea of what went wrong, but to discover what code caused the Exception, skip down the stack trace looking for something in the package com.
The function printStackTrace() of the Exception class can take one parameter, either a PrintStream or a PrintWriter. Thus, it is possible, using a StringWriter, to print the stack trace into a String: StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e. printStackTrace(pw);
Here is a quick-and-dirty grep expression... if you are using a logger such as log4j than the first line of the exception will generally contain WARN
or ERROR
, the next line will contain the Exception name, and optionally a message, and then the subsequent stack trace will begin with one of the following:
"\tat"
(tab + at)"Caused by: "
"\t... <some number> more"
(these are the lines that indicate the number of frames in the stack not shown in a "Caused by" exception)We want to get all of the above lines, so the grep expression is:
grep -P "(WARN|ERROR|^\tat |Exception|^Caused by: |\t... \d+ more)"
It assumes an Exception class always contains the word Exception
which may or may not be true, but this is quick-and-dirty after all.
Adjust as necessary for your specific case.
You can write this yourself pretty easily. Here is the pattern:
"\n\tat "
(that's new line, tab, at
, blank) This is a pretty uncommon string outside of stack traces.Now all you need to do is find the first line that doesn't start with \t
to find the end of the stack trace. You may want to skip 1-3 lines after that to catch chained exceptions.
Plus add a couple of lines (say 10 or 50) before the first line of the stack trace to get some context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With