groovyplayground
groovyplayground copied to clipboard
println only works in root context of the script
Using a println
statement inside a class will log to the server and not capture the output and send it to the client.
I found a work-around for this. I use Closures, with the following sample
// Define a closure.
def printLine = {println(it)}
class example {
private def _cl = null
public example(cl) {
_cl = cl
}
public def printTest(String value) {
_cl(value)
}
}
//this should print "test" as expected.
def test = new example(printLine);
test.printTest("test")
In case anyone else is using this service regularly and need to conveniently print lines in a class...