Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log as much information as possible for an exception in Java?

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.

Most code I've seen logs an exception by using either getMessage() or toString(). But these methods don't always capture all the information needed to pinpoint the problem - other methods such as getCause() and getStackTrace() sometimes provide additional info.

For example, the exception I'm looking at right now in my Eclipse Inspect window is an InvocationTargetException. The exception itself has no cause, no message, no stacktrace ... but the target from getCause() is InvalidUseOfMatchersException, with these details populated.

So my question is: Given an exception of any type as an input, please provide a single method that will output a nicely formatted string containing all relevant information about the Exception (e.g. possibly recursively calling getCause() 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...?

Please don't point me at any particular logging or utility framework to do this. I'm looking for a fragment of code rather than a library since I don't have the right to add external dependencies on the project I'm working on and it's actually being logged to part of a webpage rather than a file. If it's a case of copying the code fragment out of such a framework (and attributing it) that's fine :-)

like image 330
Steve Chambers Avatar asked Jan 30 '13 14:01

Steve Chambers


People also ask

How do I log an entire exception in Java?

Log Further up the Stack This can even be done using Thread. setDefaultUncaughtExceptionHandler(), which lets you specify an exception handler for uncaught exceptions for the entire application.

How do I log exceptions in logger?

Best Practice: Log the exception object. log. error("Hello again", ex); By logging the developer message together with the whole exception object, we can see the developer message is printed first, followed by the exception name and message, then the stack trace.

Should you log before throwing exception?

Log it when you handle it So, better only log the exception when you handle it. Like in the following code snippet. The doSomething method throws the exception. The doMore method just specifies it because the developer doesn't have enough information to handle it.

Where do you log exceptions?

Better to log it at the point where you are actually doing something about it, or, in the case of a library, at the boundary where it transitions into the user's code. That said, I always try to log at the point where I am throwing the exception and the condition that triggered the exception.


1 Answers

The java.util.logging package is standard in Java SE. Its Logger includes an overloaded log method that accepts Throwable objects. It will log stacktraces of exceptions and their cause for you.

For example:

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); 

Will log:

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.

like image 96
flup Avatar answered Sep 23 '22 10:09

flup