[ACCEPTED]-How do I redirect output to stderr in groovy?-groovy

Accepted answer
Score: 25

Just off the top of my head couldn't you 2 do a bit of self-wiring:

def printErr = System.err.&println
printErr("AHHH")

but that is a bit 1 manual

Score: 20

Groovy has access to the JRE:

System.err.println "goes to stderr"

Although there 1 may be a more Groovy-fied way...

Score: 19

Another quite compact alternative is this:

System.err << "Want this to go to stderr"

Or 3 you could add this at the top of your script

def err = System.err
...
err << "Want this to go to stderr"

which 2 is what I'm now doing in my groovy shell 1 scripts

Score: 1

If you just want something shorter to type, here 12 are two options. First, you can import 11 java.lang.System as anything you like, specifically 10 something shorter like "sys":

import java.lang.System as sys
sys.err.println("ERROR Will Robinson")

Second, you 9 can assign the System.err stream to a variable 8 and use that variable from then on as an 7 alias for System.err, like:

err = System.err
err.println("ERROR again Will Robinson")

This has the 6 possible advantage that all the functions 5 of System.err are accessible so you don't 4 have to wire up each one individually (e.g. err.print, err.println, etc.).

Hopefully 3 there is a standard Groovy way, because 2 idiosyncratic renaming can be confusing 1 to people who read your code.

More Related questions