How do I log a stacktrace using java's Logger class
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()
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);
Also another alternative would be:
import org.apache.commons.lang3.exception.ExceptionUtils;
log.error("Exception : "+ExceptionUtils.getStackTrace(exception));