Write messages to file & console using logger.info in jenkins pipeline

Hi All,
Am trying to write different levels of information using logger.info in jenkins pipeline. But problem is file is getting created but information is not been written to file.
i have tried using sh cmd and echo which works but via logger.info is not working.
checked jvm options in node configurations and file permissions as well. all seems to be correct.
but somehow writing info to file & console is not working.
can anyone suggest how to fix this issue ??

thanks in advance.

regards,
N RAkshitha

Hello and welcome to this community, @rakshitha. :wave:

To write different levels of information using logger.info in a Jenkins pipeline, you need to make sure that the logger is properly configured and that the log messages are being directed to the correct file. :thinking:

Here is an example of how to configure and use a logger in a Jenkins pipeline:

  1. Make sure that the logger is properly configured in your Jenkins pipeline script.
  2. Use the logger to write information to the file.

Here is an example of how to do this in a Jenkins pipeline:

import java.util.logging.Logger

pipeline {
    agent any
    stages {
        stage('Setup Logger') {
            steps {
                script {
                    // Configure the logger
                    def logger = Logger.getLogger("MyLogger")
                    def fileHandler = new java.util.logging.FileHandler("/tmp/jenkins.log", true)
                    def simpleFormatter = new java.util.logging.SimpleFormatter()
                    fileHandler.setFormatter(simpleFormatter)
                    logger.addHandler(fileHandler)
                    logger.setUseParentHandlers(false)

                    // Write log messages
                    logger.info("This is an info message")
                    logger.warning("This is a warning message")
                    logger.severe("This is a severe message")
                }
            }
        }
    }
}

As I had to add these approved signatures to make it work:

method java.util.logging.Handler setFormatter java.util.logging.Formatter
method java.util.logging.Logger addHandler java.util.logging.Handler
method java.util.logging.Logger info java.lang.String
method java.util.logging.Logger setUseParentHandlers boolean
method java.util.logging.Logger severe java.lang.String
method java.util.logging.Logger warning java.lang.String
new java.util.logging.FileHandler java.lang.String boolean
new java.util.logging.SimpleFormatter
staticMethod java.util.logging.Logger getLogger java.lang.String

I highly doubt that’s the right way to do it. :person_shrugging:

these i tried. still observing same issue. echo cmds are writing statements to file but not logger.info. i have checked status , which seems to be correct. any suggestion on how to tackle issue further ???

thanks & regards,
Rakshitha

am using this script…

import java.util.logging.ConsoleHandler
import java.util.logging.FileHandler
import java.util.logging.Level
import java.util.logging.Logger
import java.util.logging.SimpleFormatter


node('Finland-Cloud5-101') {
    echo "ECHO :: Initializing basic logging process"

    def logFilePath = "${WORKSPACE}/jenkins-logging-try1.log"
    def logger = Logger.getLogger("MyLogger")
    
    // Console Handler setup (for Jenkins console output)
    def consoleHandler = new ConsoleHandler()
    consoleHandler.setLevel(Level.ALL)
    consoleHandler.setFormatter(new SimpleFormatter())
    logger.addHandler(consoleHandler)

    // File Handler setup (for file output in Jenkins workspace, using overwrite mode for Option 2)
    def fileHandler = new FileHandler(logFilePath, false)  // false = overwrite mode
    fileHandler.setLevel(Level.ALL)
    fileHandler.setFormatter(new SimpleFormatter())
    logger.addHandler(fileHandler)

    logger.info("This is an informational message.")
    logger.severe("This is a message.")
    logger.warning("This is a warning message.")
    echo "Done"
}```
observation here is , file is getting generated in master /var/log/jenkins. but not in slave. permissions i have checked. it seems to be fine. 

please suggestion how to tackle this issue ? and write info to slave.

you have to consider that the code you run inside the node block is executed on the controller and not the agent as you might think. The sh step knows that it runs in the context of the node step and then takes care to execute the script on the agent.

node('Finland-Cloud5-101')
``` which is mentioned in script is mentioning that script is running in agent. but still file creation and messages is getting printed in /var/log/jenkins in master. 
is there anything related to permissions ? 
please do suggest places where this need to verified to fix this issue.

thanks & regards,
Rakshitha