001 package org.hackystat.utilities.stacktrace; 002 003 004 import java.io.PrintWriter; 005 import java.io.StringWriter; 006 007 /** 008 * Provides a simple solution to the common problem of obtaining a String containing the stack 009 * trace produced by an exception. 010 * <p> 011 * Call StackTrace.toString(e) to get the string corresponding to the Exception e. 012 * 013 * @author Philip Johnson, Takuya Yamashita 014 */ 015 public final class StackTrace { 016 017 /** Disable public constructor. */ 018 private StackTrace() { 019 // do nothing 020 } 021 022 /** 023 * Converts the Throwable.getStackTrace to a String representation for logging. 024 * @param throwable The Throwable exception. 025 * @return A String containing the StackTrace. 026 */ 027 public static String toString(Throwable throwable) { 028 StringWriter stringWriter = new StringWriter(); 029 throwable.printStackTrace(new PrintWriter(stringWriter)); 030 return stringWriter.toString(); 031 } 032 }