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发布于 2019-07-28 08:26:08
我在github 在这里输入链接描述上给出了一个答案--但是在这里重新发布可能是有用的。
Apache客户端使用commons-logging。它的logging API并不直接与logback集成。您将需要添加一个额外的依赖项[org.slf4j/jcl-over-slf4j "2.0.0-alpha0"]。这个jar将允许您的代码在这个API链中登录。
Apache HTTP Client -> commons-logging API -> slf4j API -> logback发布于 2019-08-01 11:44:08
为了扩展Ray的答案并防止其他日志记录丢失,可以通过添加以下依赖项,通过Logback重定向其他日志外观java.utils.logging和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"]发布于 2019-07-27 18:30:27
您是否尝试按照https://github.com/dakrone/clj-http/blob/3.x/examples/logging-apache-requests.clj显式地在运行时设置日志级别?
https://stackoverflow.com/questions/57233354
复制相似问题