Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tool to extract java stack traces from log files [closed]

Tags:

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.

like image 521
Andrey Adamovich Avatar asked May 24 '11 08:05

Andrey Adamovich


People also ask

How can I get the current stack trace in Java?

You can use Thread. currentThread(). getStackTrace() . That returns an array of StackTraceElement s that represent the current stack trace of a program.

How do I recover stack trace?

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.

How do I Analyse Java stack trace?

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.

How do I print exception stack trace in log file?

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


2 Answers

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:

  1. "\tat" (tab + at)
  2. "Caused by: "
  3. "\t... <some number> more" (these are the lines that indicate the number of frames in the stack not shown in a "Caused by" exception)
  4. An Exception name (and perhaps message) before the stack

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.

like image 104
Raman Avatar answered Dec 01 '22 01:12

Raman


You can write this yourself pretty easily. Here is the pattern:

  1. Open file
  2. Search for the string "\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.

like image 26
Aaron Digulla Avatar answered Dec 01 '22 01:12

Aaron Digulla