Unable to log from within class definitions

Skip to first unread message

Gregory Paciga

Jun 11, 2021, 4:47:02 PM6/11/21
to job-dsl-plugin
I've set up some template utilities for jobs as described here: https://github.com/jenkinsci/job-dsl-plugin/wiki/Real-World-Examples#import-other-files-ie-with-class-definitions-into-your-script

so all my actually job definitions look like:

import templates.TempletizedJob
new TempletizedJob(pipelineJob('myjob'), args)

I would like the TempletizedJob class to be able to log details of what is being configured, with the end goal being that I can use the log of the Job DSL seed job to answer questions like "how many jobs used feature X of TempletizedJob". (where "feature X" isn't something that can't just be easily grepped from the JobDSL definitions themselves).

Naively, I would just use "println" in the TemplatizedJob class, but this results in an error:
ERROR: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods println java.lang.Object java.lang.Object

I've also tried just instantiating a logger directly in the template class per https://github.com/jenkinsci/job-dsl-plugin/wiki/Job-DSL-Commands#logging
but that also throws security exceptions:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod java.util.logging.Logger getLogger java.lang.String

Even if I pass `out` as an argument in my constructor from the job dsl script, I get:
ERROR: Scripts not permitted to use method java.io.PrintStream println java.lang.String

Is there any supported way of logging from utility classes that are imported into Job DSL scripts?

mohit kumar Sharma

Jun 13, 2021, 2:06:47 AM6/13/21
to job-dsl-plugin
To consume any scripting functionality inside your class, you should have a reference to it. You can pass the `this` from your main DSL script where you are calling your class to your constructor and then internally you will be calling script methods using that ref variable.

Gregory Paciga

Jun 14, 2021, 5:26:25 PM6/14/21
to job-dsl...@googlegroups.com
Passing "this" results in the same error:
ERROR: Scripts not permitted to use method groovy.lang.Script println java.lang.Object

You received this message because you are subscribed to a topic in the Google Groups "job-dsl-plugin" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/job-dsl-plugin/nnsAZix_w0k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to job-dsl-plugi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/f1097505-392d-4b18-ae8f-4a464c3601f6n%40googlegroups.com.

Gregory Paciga (he/him)
Lead Developer, DevOps & CI/CD Tools
CBC Digital Strategy & Products

Reply all
Reply to author
0 new messages