Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print a Groovy stack trace?

Tags:

groovy

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.

like image 398
dromodel Avatar asked Jun 06 '11 23:06

dromodel


People also ask

How do I print a whole stack trace?

In order to print the stack trace in Java, we use the java. lang. Throwable. printStackTrace() method.

How do I print a string stack trace?

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.

How do I print groovy messages?

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.

Should we print stack trace?

printStackTrace(…) should never be called. Generic exceptions Error, RuntimeException, Throwable and Exception should never be thrown.


2 Answers

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.

like image 161
amccormack Avatar answered Sep 22 '22 12:09

amccormack


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'   } } 
like image 26
Shorn Avatar answered Sep 18 '22 12:09

Shorn