How can I logtrace use java logger class

I am using the Java Logger class. I want to pass ex.printStackTrace() to Logger.log(loglevel, String) , but printStackTrace() returns void . Therefore, I cannot pass and print an exception stack trace.

Is there a way to convert void to String , or are there any other methods for printing whole exception traces?

+19
source share
10 answers

You need to understand that void is actually nothingness . You cannot transform what is nothing. You can finish printing void as a string, but (trust me) you don't want this.

I think you are looking

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

This will result in an error message using the registrar you configured. For more details, you can take a look at java docs for Exception # getMessage ()

+31
source

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

 LOGGER.log(Level.INFO, ex.getMessage(), ex); 
+18
source

Also another alternative would be:

 import org.apache.commons.lang3.exception.ExceptionUtils; log.error("Exception : "+ExceptionUtils.getStackTrace(exception)); 
+7
source

You cannot convert void to String ; such a conversion does not exist. void returns nothing, so you cannot get a value.

What you probably want to do is get an exception message instead of ex.getMessage() .

+6
source

You can use the getStackTrace () method to get an array of StackTraceElements and generate a String. Otherwise, if only the final error message is enough, use the getMessage() method suggested by Makoto.

To get the stack trace as a String from an array of StackTraceElement objects, you need to iterate over the array (taken from the JDK7 source):

 StringBuilder builder = new StringBuilder(); StackTraceElement[] trace = getOurStackTrace(); for (StackTraceElement traceElement : trace) builder.append("\tat " + traceElement + "\n"); 

Another option is to use printStackTrace(PrintStream s) , where you can specify where you want to print the stack:

 ByteArrayOutputStream out1 = new ByteArrayOutputStream(); PrintStream out2 = new PrintStream(out1); ex.printStackTrace(out2); String message = out1.toString("UTF8"); 
+2
source

As Makoto says, you probably want to do ex.getMessage ().

To clarify, void means nothing is returned. You cannot use nothing :)

0
source

you can convert stacktrace to String using below. If e is an exception

 StringWriter stringWriter= new StringWriter(); PrintWriter printWriter= new PrintWriter(stringWriter); e.printStackTrace(printWriter); String stackTraceAsString= stringWriter.toString(); 
0
source

There is an overloaded printStackTrace method that uses PrintWriter.

You can do something like this

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

Thanks to everyone. I can log stack trace data using

 LOGGER.log(Level.INFO, ex.getMessage(),ex); //ex is my exception object 
0
source

With the format below, you can have a 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 a stack trace.

0
source

All Articles