Home > Java > Capturing Java Stacktrace as a String

Capturing Java Stacktrace as a String

August 9th, 2008

Here is a little code snippet for a method that will capture an Exception’s stacktrace as a string so you can output it on the command line or in a simple log statement.

Categories: Java Tags:
  1. Rahul
    August 11th, 2008 at 00:57 | #1

    log4j Logger has a method that takes Throwable as one of the arguments and prints the stacktrace. So this is no longer necessary if you are using log4j.

    http://logging.apache.org/log4j/1.2/apidocs/index.html

  2. August 11th, 2008 at 07:52 | #2

    This is true, however, I have been on at least one clients site where they refused to use Log4j and had rolled their own logging mechanism which was not able to handle Throwables. It is also amazing how many people do not pass the throwable to log4j and those the stacktrace. it makes it very hard to debug later on without that information in the log file.

  3. August 12th, 2008 at 10:26 | #3

    Thanks! Saved me some time figuring that out today. Damn java io classes!

Comments are closed.