I have a basic Groovy script, and I'm looking to create logs as simply as possible. I want the message to go to stdout, as well as a log file, and each entry in the log file would have a timestamp.
I can't use the @Log notation, because it's a script, and I don't have a class to inject into. This would have been ideal otherwise I think.
Using the
@Slf4j
annotation necessitates two lines of code: theimport
statement and the annotation itself. Here, in two lines, is how you get a logger in a Groovy script:Here's my attempt at creating a minimal example for several logback features including logging to a file. Extending @Mark O'Connor's answer above
foo.groovy:
logback-test.xml or logback.xml:
See logback documentation also
Using Log annotation is the simplest way to enable logging in groovy. Combine this with a Grape annotation to pull down the logging framework and you have everything you need in one script:
You can have the below pattern in your script (tried in Groovy Editor).
The above logs output to STDOUT. In order to log it to a file, you have to create a logger using
getLogger
. Follow the API for your convenience.