Convert exception stack trace into String

If you have a log system, such as slf4j or log4j, sometimes you want to print the exception’s stack trace into log system (a file, a console or something else). Default behavior of e.printStackTrace() is to print the stack trace into stdout.

The easiest way is to convert the stack trace into string then call logger.debug() to print it into log system.

How to do it? Here is a simple solution to it.

This sample also show you the flexability of OutputStream.