Showing posts with label stacktrace. Show all posts
Showing posts with label stacktrace. Show all posts

Monday, October 25, 2010

capture stack trace in string

So may of you want to see the complete stack trace in logs. Not happy with Exception.getMessage(). So here is a way to convert the stack trace to string so now you can enjoy the printStackTrace functionality in your logs

/**
* Capture Stack Trace into a String
*/
public static String captureStackTrace(Throwable exception)
{
StringBuffer stackTrace = new StringBuffer();
stackTrace.append("Exception in thread \"");
stackTrace.append(Thread.currentThread());
stackTrace.append("\" ");
stackTrace.append(exception);
stackTrace.append("\n");

StackTraceElement elements[] = exception.getStackTrace();
for (int x = 0; x < elements.length; x++)
{
stackTrace.append(elements[x].toString());
stackTrace.append("\n");
}
return stackTrace.toString();
}