title: How to log exceptions in Java?
body: <p>There's a common problem I've come across a few times when logging exceptions in Java. There seem to be various different types to deal with. E.g. some wrap other exceptions and some don't have a message at all - only a type.</p>
  <p>Most code I've seen logs an exception by using either <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#getMessage--" rel="nofollow noreferrer"><code>getMessage()</code></a> or <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#toString--" rel="nofollow noreferrer"><code>toString()</code></a>. But these methods don't always capture all the information needed to pinpoint the problem - other methods such as <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#getCause--" rel="nofollow noreferrer"><code>getCause()</code></a> and <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#getStackTrace--" rel="nofollow noreferrer"><code>getStackTrace()</code></a> sometimes provide additional info.</p>
  <p>For example, the exception I'm looking at right now in my Eclipse Inspect window is an <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/InvocationTargetException.html" rel="nofollow noreferrer"><code>InvocationTargetException</code></a>. The exception itself has no cause, no message, no stacktrace ... but the target from getCause() is <a href="https://www.javadoc.io/static/org.mockito/mockito-core/2.5.4/org/mockito/exceptions/misusing/InvalidUseOfMatchersException.html" rel="nofollow noreferrer"><code>InvalidUseOfMatchersException</code></a>, with these details populated.</p>
  <p>So my question is: Given an exception of <em>any</em> type as an input, please provide a single method that will output a nicely formatted string containing <em>all</em> relevant information about the Exception (e.g. possibly recursively calling <code>getCause()</code> amongst other things?) Before posting, I was nearly going to have a stab at it myself but really don't want to reinvent the wheel - surely such a thing must have been done many times before...?</p>