Tales of an Ex-Googler

Converting Java stack trace to string

Posted in Uncategorized by jeansworld on July 19, 2010

If you have an Exception e, you might want to convert the stackTrace to a string to add to your logs.  Here’s a quick code sample I found to do so from JavaPractices:

  public static String getStackTrace(Throwable aThrowable) {
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    aThrowable.printStackTrace(printWriter);
    return result.toString();
  }
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.