Enabling Trace Logging for Elasticsearch REST Client with Logback

Recently I had some issues with Elasticsearch - all requests were failing with “bad request” error. In order to understand what was wrong with these requests, I, natually, decided to enable debug/trace logging of for ES REST Client, but couldn’t find out how. Partially, because the official documentation on this topic could have been more informative, to be honest. But mainly, because my project uses Logback and the REST Client package uses Apache Commons Logging.

This article is a short summary of how I’ve eventually managed to enable tracing with Logback. The patient under inspection is Elasticsearch 6.3 with its Java Low Level REST Client.

According to the official documentation, we need to enable trace logging for the tracer package. If you are interested, you can check the source code for org.elasticsearch.client.RequestLogger class, where the logger with this name is defined:

...
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
...
private static final Log tracer = LogFactory.getLog("tracer");
...

As you can see, enabling this logger with TRACE level in Logback is not enough, because, again, the client uses Apache Commons Logging.

Luckily, Logback was designed with this use case in mind, and provides a set of bridging modules. They allow us to use Logback even with other dependencies that rely on other logging API. In particular, we’re looking for jcl-over-slf4j.jar.

So, here are the steps.

Require jcl-over-slf4j.jar. The dependencies section for Gradle:

dependencies {
    implementation('org.slf4j:slf4j-api:1.8.0-beta2')
    implementation('ch.qos.logback:logback-classic:1.3.0-alpha4')
    implementation('org.slf4j:jcl-over-slf4j:1.8.0-beta2')
}

Exclude commons-logging.jar. The details of why are described in the Logback docs here.

dependencies {
    configurations.all {
        exclude group: "commons-logging", module: "commons-logging"
    }
}

Enable tracer logger in Logback configuration.

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[%d{ISO8601}] [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="WARN">
        <appender-ref ref="STDOUT" />
    </root>

    <logger name="tracer" level="TRACE" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>

    <!-- Additionally, you can also enable debug logging ofr RestClient class itself -->
    <logger name="org.elasticsearch.client.RestClient" level="DEBUG" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>
</configuration>

Voilà! Enjoy your debugging session!

Share this page on Twitter or Reddit