使用记录back的clj-http记录



clj-http说明了如何使用log4j2设置日志记录,但是我的项目使用logback,我无法设法从clj-http使用的基础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})

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"}))

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>

执行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

我在GitHub上给出了一个答案,请在此处输入链接描述 - 但是将其重新放置在此处可能很有用。

Apache HTTP客户端使用commons-logging。它正在记录API直接与logback集成。您将需要添加其他依赖项[org.slf4j/jcl-over-slf4j "2.0.0-alpha0"]。该罐子将允许您的代码通过此API链登录。

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

以扩展Ray H的答案并防止其他日志记录丢失,您可以通过添加这些依赖项来重定向其他日志记录外墙Java.utils.logging和log4j,而Log4J:

[ch.qos.logback/logback-classic "1.2.3" :exclusions [org.slf4j/slf4j-api]]
[org.slf4j/slf4j-api "1.7.26"]
[org.slf4j/jul-to-slf4j "1.7.25"]
[org.slf4j/jcl-over-slf4j "1.7.25"]
[org.slf4j/log4j-over-slf4j "1.7.26"]

您是否尝试按https://github.com/dakrone/clj-http/blob/3.x/examples/logging-apache-requests明确尝试在运行时明确设置日志级别.clj?

最新更新