带有 logback 的 clj-http 日志记录

lit*_*ude 3 logging clojure logback

clj-http解释了如何使用 设置日志记录log4j2,但我的项目使用 logback,我无法从clj-http.

这是我正在做的事情的简约再现。

之后lein new test-logging,我编辑project.clj如下:

(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})
Run Code Online (Sandbox Code Playgroud)

src/test_logging/core.clj我只有:

(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"}))
Run Code Online (Sandbox Code Playgroud)

logback配置文件下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>
Run Code Online (Sandbox Code Playgroud)

这样做时,lein run我希望看到来自 http 客户端的日志,但我只有这个:

 $ 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
Run Code Online (Sandbox Code Playgroud)

Ray*_*y H 5

我在 github 上给出了一个答案,请在此处输入链接描述——但是在此处重新发布它可能会很有用。

Apache HTTP 客户端使用commons-logging. 它的日志 API 不logback直接集成。您将需要添加额外的依赖项[org.slf4j/jcl-over-slf4j "2.0.0-alpha0"]。这个 jar 将允许您的代码通过这个 API 链进行记录。

Apache HTTP Client -> commons-logging API -> slf4j API -> logback
Run Code Online (Sandbox Code Playgroud)