How do I log a stacktrace using java's Logger class

86,440

Solution 1

You need to understand that void is actually nothingness. You cannot convert what is nothing. You might end up printing void as a string, but (trust me), you don't want that.

I think what you are looking for is

// assuming ex is your Exception object
logger.error(ex.getMessage(), ex);
// OR
Logger.log(errorLogLevel, ex.getMessage(), ex)

This will print the error message using the logger that you have configured. For more details, you can take a look at the java docs for Exception#getMessage()

Solution 2

Use java.util.logging.Logger#log(Level, String, Throwable) and pass in ex as third argument like this:

LOGGER.log(Level.INFO, ex.getMessage(), ex);

Solution 3

Also another alternative would be:

import org.apache.commons.lang3.exception.ExceptionUtils;

log.error("Exception : " + ExceptionUtils.getStackTrace(exception));

Solution 4

There's an overloaded printStackTrace method that takes in a PrintWriter.

You can do something like this

Writer buffer = new StringWriter();
PrintWriter pw = new PrintWriter(buffer);
ex.printStackTrace(pw);
Logger.log(loglevel, buffer.toString());

Solution 5

With below format you can have the stack trace:

java.util.logging.SimpleFormatter.format=%1$tF %1$tT [%4$-7s][%2$s] %5$s %6$s%n

The point in this pattern is %6$s. It will print the stack trace.

Share:
86,440
Surya Joseph
Author by

Surya Joseph

Updated on July 09, 2022

Comments

  • Surya Joseph
    Surya Joseph almost 2 years

    I am using Java's Logger class. I want to pass ex.printStackTrace() into Logger.log(loglevel, String), but printStackTrace() returns void. So I am not able to pass and print the stack trace of the exception.

    Is there any way that I can convert void into String, or are there any other methods to print the whole stack trace of exceptions?

  • beresfordt
    beresfordt almost 9 years
    Just use the apache commons ExceptionUtils library; no need to reinvent the wheel
  • Surya Joseph
    Surya Joseph almost 9 years
    Thanks Saif Asif. I am able to log the whole stack trace now.
  • Adam
    Adam over 6 years
    logger.error() is not java.util.logging, but I'm not going to downvote you because I am glad to discover logger.log(Level, Exception, Supplier) . Looks like Oracle have made java.util.logging.Logger seriously ugly! Since they were doing that, it's strange that they didn't take it further and create logger.severe(String, Exception) etc
  • Panu Haaramo
    Panu Haaramo over 6 years
    I'm not getting stack trace to log with this, only the second argument.
  • hzpz
    hzpz over 6 years
    It is possible to suppress the stack trace by changing the log format. Which formatter are you using and how does your log format look like?
  • DAB
    DAB over 5 years
    The question asks for a solution using Java's Logger class. java.util.logging.Logger does not have a method called "error". I have downvoted this answer only because of that.
  • JoschJava
    JoschJava over 5 years
    If you're refering to org.apache.commons.lang3.exception.ExceptionUtils, it's ExceptionUtils.getStackTrace(e)
  • DGoiko
    DGoiko about 5 years
    @Adam I agree that logger.error() looks ugly, but it is highly readable, and it is the way it is done in other simple languages like python, so it is not so terribly bad.
  • steinybot
    steinybot about 5 years
    This is the real answer to the question.
  • steinybot
    steinybot about 5 years
    @PanuHaaramo It depends on which formatter you use. The formatter needs to call record.getThrown() in format. Take a look at SimpleFormatter.
  • Oliv
    Oliv almost 5 years
    He wants a stack trace, not just the message.
  • Nikita
    Nikita over 4 years
    1. String message = out1.toString("UTF8"); - compilation error, expected 0 parameterers, but got 1 2. It works correctly out2.out.toString. In case out2.toString -> 'PrintStream@hash'
  • A. Gille
    A. Gille almost 4 years
    This is not answering the initial intention to print the stack trace to a logger, at all. getMessage() is not made for this.
  • Kenny
    Kenny over 3 years
    So, the answer is: logger.log(Level.SEVERE, ex.getMessage(), ex);
  • Marco Sulla
    Marco Sulla almost 3 years
    This is the simplest method IMHO and it really prints the stack trace, not only the message.