Blog Archive

Java convert stacktrace to string for logging to a database

Well in my Java project I wanted to log the exception stacktrace to a database file, so there comes the need to convert it to a string. Here is how to do it ...
public static String getMyStackTrace(Exception e) {
       
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter, true);
        e.printStackTrace(printWriter);
        return stringWriter.toString();
  
    }