This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Java in General and the fly likes Storing stacktrace of an exception to the database. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Storing stacktrace of an exception to the database." Watch "Storing stacktrace of an exception to the database." New topic
Author

Storing stacktrace of an exception to the database.

Alok Pota
Ranch Hand

Joined: Mar 07, 2001
Posts: 185
When an exception is thrown, you can print the stacktrace to the default PrintWriter (System.out) or a specified PrintWriter. However if I wanted to log the exception
stacktrace to the database how do I get the stacktrace?
I know there is a toString() function of the exception but that does not give the stack trace.
Alok Pota
Ranch Hand

Joined: Mar 07, 2001
Posts: 185
public class ExceptionWriter extends PrintWriter {
public ExceptionWriter() {
super(new StringWriter());
}
public ExceptionWriter(OutputStream out) {
super(out);
}
public ExceptionWriter(OutputStream out, boolean autoFlush) {
super(out, autoFlush);
}
public ExceptionWriter(Writer out) {
super(out);
}
public ExceptionWriter(Writer out, boolean autoFlush) {
super(out, autoFlush);
}

public StringBuffer getBuffer() {
if (super.out instanceof StringWriter) {
return ((StringWriter)out).getBuffer();
}
throw new IllegalArgumentException("Underlying writer is not a StringWriter");
}
}

to use the above
Throwable t;
ExceptionWriter ew = new ExceptionWriter();
t.printStackTrace(ew);
String stackTrace = ew.getBuffer().toString();
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Storing stacktrace of an exception to the database.
 
It's not a secret anymore!