There's a common problem I've come across a few times when logging Exceptions. There seem to be various different types to deal with. E.g. some wrap other Exceptions, some don't have a message at all - just a type.
Most code I've seen logs Exceptions using either
import java.util.logging.Level; import java.util.logging.Logger; [...] Logger logger = Logger.getAnonymousLogger(); Exception e1 = new Exception(); Exception e2 = new Exception(e1); logger.log(Level.SEVERE, "an exception was thrown", e2);
SEVERE: an exception was thrown java.lang.Exception: java.lang.Exception at LogStacktrace.main(LogStacktrace.java:21) Caused by: java.lang.Exception at LogStacktrace.main(LogStacktrace.java:20)
Internally, this does exactly what @philipp-wendler suggests, by the way.
See the source code for
SimpleFormatter.java. This is just a higher level interface.