Skip to content
richabindra edited this page Aug 31, 2018 · 1 revision

When writing and debugging an application that uses the bitbucket-rest library, it can be useful to activate logging and trace HTTP transactions. For these cases, pass a logging framework to the BitbucketClient builder modules list. For example using SLF4J, this can be done like so:

@Grab(group='com.cdancy', module='bitbucket-rest', version='2.2.1')
@Grab(group='org.apache.jclouds.driver', module='jclouds-slf4j', version='2.1.1')
@Grab(group='org.slf4j', module='slf4j-log4j12', version='1.7.25')

import com.cdancy.bitbucket.rest.BitbucketClient
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule

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

println(client.api().systemApi().version())

To control the logging, provide an SLF4J12 configuration file at runtime to the application. This configuration file could contain:

log4j.rootLogger=TRACE, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Pass this configuration file to the application using the JAVA_OPTS environment variable. For example:

$ JAVA_OPTS=-Dlog4j.configuration=file:log4j.properties path/to/app

If you wish to build logging into your application, put a log4j.properties file in its src/main/resources folder. If you use the gradle build system, it will automatically include the content of src/main/resources to the application jar when it is published.

Conditional logging

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

BitbucketClient.Builder cb = BitbucketClient.builder()
if (someCondition) {
    cb = cb.modules(new SLF4JLoggingModule())
}
BitbucketClient bc = cb.build()
println(bc.api().systemApi().version())

Determining logger versions

Determine the version to use for the jclouds-slf4j and slf4j-log4j12 modules by looking at the bitbucket-rest testCompile dependencies.

For example:

$ git clone https://github.com/cdancy/bitbucket-rest
...
$ git checkout v2.2.1
$ ./gradlew dependencies --console plain --configuration testCompile | grep "jclouds-slf4j\|slf4j-api"
+--- org.apache.jclouds.driver:jclouds-slf4j:2.1.1
|    +--- org.slf4j:slf4j-api:1.7.2 -> 1.7.25
|    \--- org.slf4j:slf4j-api:1.7.25

The versions to use for jclouds-slf4j is 2.1.1, and the version to use for slf4j-log4j12 must match the one used for slf4j-api, i.e. 1.7.25.

Clone this wiki locally