How do I log a stacktrace using java's Logger class
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.
Surya Joseph
Updated on July 09, 2022Comments
-
Surya Joseph almost 2 years
I am using Java's
Logger
class. I want to passex.printStackTrace()
intoLogger.log(loglevel, String)
, butprintStackTrace()
returnsvoid
. So I am not able to pass and print the stack trace of the exception.Is there any way that I can convert
void
intoString
, or are there any other methods to print the whole stack trace of exceptions? -
beresfordt almost 9 yearsJust use the apache commons ExceptionUtils library; no need to reinvent the wheel
-
Surya Joseph almost 9 yearsThanks Saif Asif. I am able to log the whole stack trace now.
-
Adam over 6 years
logger.error()
is notjava.util.logging
, but I'm not going to downvote you because I am glad to discoverlogger.log(Level, Exception, Supplier)
. Looks like Oracle have madejava.util.logging.Logger
seriously ugly! Since they were doing that, it's strange that they didn't take it further and createlogger.severe(String, Exception)
etc -
Panu Haaramo over 6 yearsI'm not getting stack trace to log with this, only the second argument.
-
hzpz over 6 yearsIt 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 over 5 yearsThe 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 over 5 yearsIf you're refering to org.apache.commons.lang3.exception.ExceptionUtils, it's ExceptionUtils.getStackTrace(e)
-
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 about 5 yearsThis is the real answer to the question.
-
steinybot about 5 years@PanuHaaramo It depends on which formatter you use. The formatter needs to call
record.getThrown()
informat
. Take a look atSimpleFormatter
. -
Oliv almost 5 yearsHe wants a stack trace, not just the message.
-
Nikita over 4 years1.
String message = out1.toString("UTF8");
- compilation error, expected 0 parameterers, but got 1 2. It works correctlyout2.out.toString
. In caseout2.toString
-> 'PrintStream@hash' -
A. Gille almost 4 yearsThis is not answering the initial intention to print the stack trace to a logger, at all. getMessage() is not made for this.
-
Kenny over 3 yearsSo, the answer is: logger.log(Level.SEVERE, ex.getMessage(), ex);
-
Marco Sulla almost 3 yearsThis is the simplest method IMHO and it really prints the stack trace, not only the message.