Logging from Custom class loaded using workflow-cps-global-lib-plugin

128 views
Skip to first unread message

Krishnakumar S

unread,
Jun 13, 2016, 4:04:38 AM6/13/16
to Jenkins Developers
All,
 I am trying to use  workflow-cps-global-lib-plugin to create some custom classes to be used within pipeline Job for some specific use case requirement. 

Instantiating the custom class within the pipeline Job. However anything logged within the our custom class is not posted in the Jenkins console.

As a hack, I have tried sending the Listener of the build to the custom class and logging it to it which solves the issue of messages not appearing in the console. However, message get jumbled up and down, as every step within the pipeline is executed in separate context and the log messages sent to from custom class ends up on top of all the messages, which defeats the purpose. see example below.

Is there any other alternatives that we could potentially use, such as creating a workflow step to execute the methods of our custom class, which passes the listener from the correct context.


Appreciate any suggestions, that could help us get over this hurdle as we are running tight timelines to this done. Thanks for your help.


Here is the sample code;

In Pipeline job definition:

def custom_class = new CustomClass(currentBuild.getRawBuild())
echo "starting Master"
custom_class.test_log()
echo "Test Echo"


CustomClass, logger is defined as below.
class CustomClass {
def jenkins_logger

CustomClass(rawbuild) {
this.jenkins_logger = rawbuild.listener.getLogger()
}
def test_log(msg) {
this.jenkins_logger.println(msg)
}
}



Jenkins Console Log

Started by user anonymous
Replayed #14
[Pipeline] node
Running on master in /var/lib/jenkins/jobs/logger_test/workspace@3
Test Message from Custom Class method  --> This step is executing after echo step below, but message appears on the Top of the Log.
[Pipeline] {
[Pipeline] echo
starting Master --> This is echo step within the Pipeline Job
[Pipeline] echo
Test Echo  -> this is executed after Custom class method.
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Jesse Glick

unread,
Jun 13, 2016, 12:06:35 PM6/13/16
to Jenkins Dev
On Mon, Jun 13, 2016 at 4:04 AM, Krishnakumar S <kris...@gmail.com> wrote:
> def custom_class = new CustomClass(currentBuild.getRawBuild())
> echo "starting Master"
> custom_class.test_log()
> echo "Test Echo"
>
>
> CustomClass, logger is defined as below.
> class CustomClass {
> def jenkins_logger
>
> CustomClass(rawbuild) {
> this.jenkins_logger = rawbuild.listener.getLogger()
> }
> def test_log(msg) {
> this.jenkins_logger.println(msg)
> }
> }

Too much effort, and cannot be made to work this way anyway.

class CustomClass {
Script script
def whatever() {
script.echo 'hello'
}
}

new CustomClass(script: this).whatever()

KrishnaKumar Sivashanmugam

unread,
Jun 13, 2016, 7:50:34 PM6/13/16
to jenkin...@googlegroups.com
Jesse, you made my day, Thanks a lot, this works great!!




--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/iQRrVFUOICg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CANfRfr1%2BUZOC5WSbfDoXK%3DLDcHJEGh2vXYk-QkGigSyh8sG8qg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Jesse Glick

unread,
Jun 15, 2016, 3:57:17 PM6/15/16
to Jenkins Dev
On Mon, Jun 13, 2016 at 7:50 PM, KrishnaKumar Sivashanmugam
<kris...@gmail.com> wrote:
> Jesse, you made my day, Thanks a lot, this works great!!

Cool, return the favor and write up a nicely formatted question on
http://stackoverflow.com/questions/tagged/jenkins-workflow (you are
not just allowed, but encouraged, to answer your own questions); or
file a PR to offer more detailed suggestions in
https://github.com/jenkinsci/workflow-cps-global-lib-plugin/blob/master/README.md
Reply all
Reply to author
Forward
0 new messages