Skip to content

Logging

Martin d'Anjou edited this page Jun 15, 2022 · 3 revisions

When writing and debugging an application that uses the jenkins-rest library, it can be useful to activate logging and trace HTTP transactions. For these cases, pass a logging framework to the JenkinsClient builder modules list. The jclouds recommended logging framework is logback.

For example, this can be done like so:

@Grab(group='io.github.cdancy', module='jenkins-rest', version='1.0.1')
@Grapes([
    @GrabConfig(systemClassLoader=true),
    @Grab(group='org.apache.jclouds.driver', module='jclouds-slf4j', version='2.5.0'),
    @Grab(group='ch.qos.logback', module='logback-classic', version='1.2.11')
])

import com.cdancy.jenkins.rest.JenkinsClient
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule

JenkinsClient client = JenkinsClient.builder()
    .modules(new SLF4JLoggingModule())
    .build()

println(client.api().systemApi().systemInfo())

To control the logging, provide a logback.xml configuration file at runtime to the application. This configuration file could contain:

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="trace">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Pass this configuration file to the application using the JAVA_OPTS environment variable. Using the above groovy script, pass the configuration file like so:

groovy -Dlogback.configurationFile=logback.xml script.groovy

For more on how to configure logback, please see the logback manual.

Conditional logging

The logging module can be passed based on some run time condition as well:

JenkinsClient.Builder cb = JenkinsClient.builder()
if (someCondition) {
    cb = cb.modules(new SLF4JLoggingModule())
}
JenkinsClient jc = cb.build()
println(jc.api().systemApi().systemInfo())

Determining logger versions

Determine the version to use for the jclouds-slf4j and logback-classic modules by looking at the jenkins-rest testCompileClasspath dependencies.

For example:

$ git clone https://github.com/cdancy/jenkins-rest
...
$ git checkout v1.0.0
$ ./gradlew dependencies --configuration testCompileClasspath | grep "jclouds-slf4j\|logback"
+--- org.apache.jclouds.driver:jclouds-slf4j:2.5.0
+--- ch.qos.logback:logback-core:1.2.11
\--- ch.qos.logback:logback-classic:1.2.11
     +--- ch.qos.logback:logback-core:1.2.11

The versions to use for jclouds-slf4j is 2.5.0, and the version to use for logback-classic is 1.2.11.

Clone this wiki locally