1

I've been using HttpBuilder-NG in a large compiled project and relying on its request and response logging when debugging, but using it in a standalone Groovy script results in only output from my class, not the library's logs.

Can anyone suggest how to get those logs I'm expecting to see from the HttpBuilder-NG library being used?

Here's a quick test scenario I put together:

// logging-test.groovy

import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.Level

import static groovyx.net.http.HttpBuilder.configure
import static groovyx.net.http.util.SslUtils.ignoreSslIssues

import groovy.util.logging.Slf4j
import groovyx.net.http.OkHttpBuilder

@GrabConfig(systemClassLoader = true) // Encounters class loading issues without this
@GrabResolver(name = 'mcArtifacts', root = 'https://artifactory.mycompany.com/artifactory/maven-all/')
@Grab(group = 'io.github.http-builder-ng', module = 'http-builder-ng-core', version = '1.0.3')
@Grab(group = 'io.github.http-builder-ng', module = 'http-builder-ng-okhttp', version = '1.0.3')
@Grab('ch.qos.logback:logback-classic:1.2.3')
@Grab('ch.qos.logback:logback-core:1.2.3')

@Slf4j
class LoggingTest {

private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(LoggingTest.class)

    static void main(String[] args) {
      new LoggingTest().run(args)
    }

    def run(String[] args) {

        def builder = OkHttpBuilder.configure({
            ignoreSslIssues execution
            request.uri = "https://dummy.restapiexample.com"
        })
        def currentContents = builder.get {
            request.uri.path = "/api/v1/employees"
        }

        LOGGER.info "Testing output - HttpBuilder-NG gives back a ${currentContents.getClass()}"
        LOGGER.debug "Validating debug works."
    }
}

And for logback configuration:

// logback.groovy

import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.ConsoleAppender

appender('CONSOLE', ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        pattern = '%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n'
    }
}

logger 'groovyx.net.http.JavaHttpBuilder', DEBUG, [ 'CONSOLE' ]
logger 'groovy.net.http.JavaHttpBuilder', DEBUG, [ 'CONSOLE' ]
logger 'groovy.net.http.JavaHttpBuilder.content', TRACE, [ 'CONSOLE' ]
logger 'groovy.net.http.JavaHttpBuilder.headers', DEBUG, [ 'CONSOLE' ]

root DEBUG, ['CONSOLE']

Console output on execution:

$ groovy logging-test.groovy
08:57:46.053 [main] INFO  LoggingTest - Testing output - HttpBuilder-NG gives back a class org.apache.groovy.json.internal.LazyMap
08:57:46.056 [main] DEBUG LoggingTest - Validating debug works.
jsparks
  • 150
  • 1
  • 8
  • I'm not personally familiar with httpbuilder-ng, but if you are using the OkHttp client, this site https://http-builder-ng.github.io/http-builder-ng/asciidoc/html5/#_logging says it doesn't actually have any logging. – Daniel Jul 22 '21 at 21:36
  • The combination used here does have logging; I've got this same setup in a fully compiled groovy project (compiles and has a jar executable), which gives requests and responses in the logs, quite verbose. – jsparks Jul 23 '21 at 01:42

0 Answers0