Search code examples
elasticsearchlogbackapache-commons-loggingelastic4s

Enabling ElasticSearch RestClient response logging when using elastic4s and logback


I am using elastic4s v6.5.2 to send pretty simple index/update requests to ElasticSearch.
elastic4s uses com.elasticsearch.client.RestClient under the hood, which seems to support response logging.
My goal is to incorporate these logs in my application (write them to a file/stdout).

In my project, I am using logback as the logging framework, whereas elasticsearch (and subsequently its RestClient) uses commons-logging, as far as I understand.

I have tried:

  • Adding a new <logger> in my logback configuration
    (both for the package name="org.elasticsearch.client and the specific class name="org.elasticsearch.client.RestClient")
  • Adding a commons-logging.properties file into my project's resources with any of these:

    .level=trace
    org.elasticsearch.client.level=trace
    org.elasticsearch.client.RestClient.level=trace

I have also tried different log levels - fine, trace, all.

None of these helped - elasticsearch's logger.isDebugEnabled() method always returns false.
Internally, elasticsearch's RequestLogger has a Jdk14Logger (from commons-logging) and calls its isDebugEnabled() method, which in turn has a java.util.logging.Logger and calls its isLoggable(Level.FINE) method which returns false, since it has no logging level defined.


Solution

  • The key to solving this was removing the commons-logging dependency and instead adding a dependency on jcl-over-slf4j.
    In sbt this would look like this:

    libraryDependencies += "org.slf4j" % "jcl-over-slf4j" % "1.7.25"
    excludeDependencies += "commons-logging" % "commons-logging"
    

    After this, the logger can be configured with logback as simply as:

    <logger name="org.elasticsearch.client" level="debug"/>