views:

569

answers:

6

I have a generic function that prints exceptions (using log4j):

private void _showErrorMessage(Exception e) {
    log.error(e.getClass() + ": " +  e.getMessage() + ": " + e.getCause() + "\n" +  e.getStackTrace().toString());
}

Instead of seeing the stack trace I'm seeing:

[Ljava.lang.StackTraceElement;@49af7e68

How can I view the stack trace of the exception properly?

update

log.error(e) <- shows the error, but doesn't show stack trace

+1  A: 

Throwable.getStackTrace returns an array of StackTraceElements, hence the toString method is returning a textual representation of the array itself.

In order to actually retrieve the stack trace information, one would have to go through each StackTraceElement to get more information.

coobird
+12  A: 

Your logging framework should have the ability to log exceptions, so simply passing the exception to the proper .error(Object, Throwable) call should be enough:

If your logging framework can't do that, or you need the stack trace in a String for any other reason, then it becomes a bit harder. You'll have to create a PrintWriter wrapping a StringWriter and call .printStackTrace() on the Exception:

StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();
Joachim Sauer
when i pass the exception to log.error, it just shows the error message with no stack trace. using log4j
ufk
That means that you called the wrong version. There's `error(Object)` and `error(Object,Throwable)`. Calling the first version will effectively call `toString()`.
Joachim Sauer
-1 - log4j can do it, he just invokes the wrong method.
Thorbjørn Ravn Andersen
@Thorbjørn: -1 for what/who? If you voted the -1, then please explain why. I **do** mention that log4j can do that.
Joachim Sauer
@Joachim, the way to handle exceptions with log4j - which he mentions to be using - is to log using log.error(String, Throwable) and let log4j handle the stack trace. The asker clearly isn't aware of that.
Thorbjørn Ravn Andersen
@Joachim, sorry. It appears that the log4j-information was added in the edit made after your answer.
Thorbjørn Ravn Andersen
+1  A: 

I'll use the ExceptionUtils#getFullStackTrace of jakarta commons lang

Guillaume
exactly what i needed, thanks alot!!!!!!!!!!!!!!!
ufk
+3  A: 

Have you tried?

private void _showErrorMessage(Exception e) {
    log.error("Hey! got an exception", e);
}
Paul McKenzie
+1  A: 

You could also look at the Guava libraries from Google.

Throwables.getStackTraceAsString(Throwable throwable)

gpampara
+1  A: 

The exact answer to your question is that you should call Log4J like this:

private void _showErrorMessage(Exception e) {
    log.error(e.getClass() + ": " +  e.getMessage() + ": " + e.getCause(), e);
}

Although I would dispense with the call to e.getCause() because the stacktrace will give that to you anyway, so:

private void _showErrorMessage(Exception e) {
    log.error(e.getClass() + ": " +  e.getMessage(), e);
}

ExceptionUtils is fine if you really need a string of the stacktrace, but since you are using Log4J, you lose a lot by not utilizing its built in exception handling.

Yishai