logginggroovy

Logging in Groovy Script


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.


Solution

  • You can have the below pattern in your script (tried in Groovy Editor).

    import java.util.logging.Logger
    
    Logger logger = Logger.getLogger("")
    logger.info ("I am a test info log")
    

    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.