Aleph0
Aleph0

Reputation: 6084

Log messages are not showing up in groovy jenkins script

I'm in need to extend my jenkins scripts with logging functionality and tried to use java.util.logging for this purpose. The following snippet shows what I already did.

import java.util.logging.*

@NonCPS
def tryLogging() {
        println("Start")
        Logger logger = Logger.getLogger("Test")
        logger.setLevel(Level.INFO);
        logger.setUseParentHandlers(false);
        ConsoleHandler handler = new java.util.logging.ConsoleHandler()
        handler.setFormatter(new SimpleFormatter())
        logger.addHandler(handler)      
        logger.info("Hello")
        logger.severe("Severe")
        println("End")
}

tryLogging()

My console log now says the following:

[Pipeline] Start of Pipeline
[Pipeline] echo
Start
[Pipeline] echo
End

There is really no visible log message and I don't know, what I'm doing wrong. Can anyone here explain me how to make the console log visible? I'm also unsure if I have to use this @NonCPS here?

[Pipeline] End of Pipeline Finished: SUCCESS

Upvotes: 0

Views: 1616

Answers (1)

jmehrens
jmehrens

Reputation: 11085

ConsoleHandler prints to System.err. Modify your script to use

System.err.println("Start") and see if 'Start' still shows up in the console. If it doesn't then the problem is the ConsoleHandler is writing to a stream you can't see.

Another thing to change is that you'll want to ensure your named logger can't be garbage collected. Depending the Java version you are using the logger can be garbage collected and you can lose your attached handler.

I'm also unsure if I have to use this @NonCPS here?

What is @NonCPS annotation in Jenkins pipeline script has information that you might find helpful.

Upvotes: 1

Related Questions