Search code examples
loggingclojurelogback

clj-http logging with logback


clj-http explains how to set up logging with log4j2, but my project uses logback, and I can't manage to get the logs from the underlying http client used by clj-http.

Here is a minimalistic reproduction of what I'm doing.

After lein new test-logging, I edited project.clj as follow:

(defproject test-logging "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url "https://www.eclipse.org/legal/epl-2.0/"}
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [ch.qos.logback/logback-classic "1.2.3"]
                 [org.clojure/tools.logging "0.4.1"]
                 [clj-http "3.10.0"]]
  :resource-paths ["resources"]
  :main ^:skip-aot test-logging.core
  :repl-options {:init-ns test-logging.core})

In src/test_logging/core.clj I just have:

(ns test-logging.core
  (:require [clojure.tools.logging :as log]
            [clj-http.client :as http]))

(defn -main []
  (http/post "https://httpbin.org/post" {:body "this is a test"}))

The logback configuration file is under resources/logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="10 seconds">

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </encoder>
    </appender>

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

When doing lein run I expect to see the logs from the http client, but I only have this:

 $ lein run
16:44:41,283 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
16:44:41,283 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
16:44:41,283 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/corentin/code/clojure/test-logging/resources/logback.xml]
16:44:41,442 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [file:/home/corentin/code/clojure/test-logging/resources/logback.xml] 
16:44:41,442 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 10 seconds
16:44:41,445 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
16:44:41,450 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
16:44:41,456 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
16:44:41,493 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
16:44:41,494 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
16:44:41,494 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
16:44:41,495 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@31ecb361 - Registering current configuration as safe fallback point

Solution

  • I gave an answer on github enter link description here -- however it may be useful to re-post it here.

    Apache HTTP Client uses commons-logging. It's logging API does not integrate with logback directly. You will need to add an additional dependency [org.slf4j/jcl-over-slf4j "2.0.0-alpha0"]. This jar will allow your code to log through this chain of APIs.

    Apache HTTP Client -> commons-logging API -> slf4j API -> logback