How do I print a Groovy stack trace? The Java method, Thread.currentThread().getStackTrace() produces a huge stack trace, including a lot of the Groovy internals. I'm seeing a function called twice from a StreamingMarkupBuilder that looks like it should only be called once and I would like to see why Groovy thinks it should be calling it twice.
In order to print the stack trace in Java, we use the java. lang. Throwable. printStackTrace() method.
Example: Convert stack trace to a stringIn the catch block, we use StringWriter and PrintWriter to print any given output to a string. We then print the stack trace using printStackTrace() method of the exception and write it in the writer. Then, we simply convert it to string using toString() method.
You can use the print function to print a string to the screen. You can include \n to embed a newline character. There is no need for semi-colon ; at the end of the statement. Alternatively you can use the println function that will automatically append a newline to the end of the output.
printStackTrace(…) should never be called. Generic exceptions Error, RuntimeException, Throwable and Exception should never be thrown.
Solution:
org.codehaus.groovy.runtime.StackTraceUtils.sanitize(new Exception()).printStackTrace()
Original answer:
A Google search returns the following information:
Apparently, there is a method in org.codehaus.groovy.runtime.StackTraceUtils
called printSanitizedStackTrace
. There isn't much documentation for the method, though there is a method called sanitize
which is described as
remove all apparently groovy-internal trace entries from the exception instance This modifies the original instance and returns it, it does not clone
So I would try org.codehaus.groovy.runtime.StackTraceUtils.printSanitizedStackTrace(Throwable t)
(it is static) and see if that works for you.
I found this questions when searching for "spock print full stack trace"
.
My unit tests are written in Groovy, using the Spock testing framework and they're run in the context of a Gradle build.
The fix for me was as simple as adding exceptionFormat = 'full'
to my Gradle test task specification:
test { testLogging { exceptionFormat = 'full' } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With