Can jenkins agent be made to log to STDOUT/STDERR instead of remoting to controller?

I am spinning up new containers for each jenkins agent.
Jenkins agent usually sends the logs to the controller via remoting.

Nov 27, 2022 7:45:14 AM hudson.remoting.Engine startEngine
INFO: Using Remoting version: 3077.vd69cf116da_6f
Nov 27, 2022 7:45:14 AM org.jenkinsci.remoting.engine.WorkDirManager initializeWorkDir
INFO: Using /home/jenkins/agent/remoting as a remoting work directory
Nov 27, 2022 7:45:14 AM org.jenkinsci.remoting.engine.WorkDirManager setupLogging
INFO: Both error and output logs will be printed to /home/jenkins/agent/remoting

Is it possible to make the jenkins agent to log to STDOUT ? I need the logs to show up in container logs.

Can I set global environmental variables JAVA_OPTS or JENKINS_JAVA_OPTS to change the LOG location to STDOUT?

If so what values should I set… ?

refer: remoting/logging.md at bdecda169750ecb7aaf194bf2bc303cdd015421c · jenkinsci/remoting · GitHub

Thanks

Shanthi

As a reminder, the term “slave” to refer to an agent has been deprecated since 2016. The term “master” to refer to the controller has been deprecated since 2020. Please refer to On Jenkins Terminology Updates for more details. We request you update your post.

Thanks,
Mark Waite

I’m not aware of any way to make the agent write to stdout rather than sending the logs to the controller.

1 Like

Edited as requested.

1 Like

I am not sure about completely “sending” the log, as in some sort of run-time bind that happens line-by-line (or technically flush by flush), but if you need the agent’s log to show up in the build log, you can add logic to extract it close to the end of build (e.g. when closing the agent-scoped stage if it is temporary, possibly in a post{} block). Something like agentLog = agent.getComputer().getLog(); would get you a Java String object with same content as Jenkins controller UI would show you in $JENKINS_URL/computer/agentname/log and you can echo that string, inspect its contents for anticipated errors, or writeFile and archiveArtifact it, etc.

For illustration, see Revive ability to snapshot() the CertificateCredentials so they can be used on remote agents by jimklimov · Pull Request #391 · jenkinsci/credentials-plugin · GitHub

One tricky question would be to resolve which agent you are after in a particular codepath, especially if your builds are associated with several. I am not ready to quickly answer that bit, but it probably would start with looking at thread executor context and spinning from there. Or doing that outside Jenkins, by using some unique tokens that you can match with a worker (e.g. generate an UUID into envvars set by agent spawner, look at /etc/machine-id or ssh server key, or some such).

1 Like

Yes your initial assumption is right I want it to happen in a “streaming” kind of way
Jenkins spawns agents and agents logs go to master controller
But I need the logs to go to container logs in a streaming way
So your suggestion will work only AFTER a pipeline gets completed isnt it?

In this page

it is mentioned

Default behavior with work directory

With work directory Remoting automatically writes logs to the disk. This is a main difference from the legacy mode without workDir.

Logging destinations:

  • STDOUT and STDERR
    • Logs include java.util.logging and messages printed to STDOUT/STDERR directly.

So is it possible to pipe the remoting logging to STDOUT using command line arguments?

If so can I send those command line arguments through jenkins environmental variable JAVA_OPTS etc?

Thanks

Shanthi