gpt4 book ai didi

logging - 带有 logback 的 clj-http 日志记录

转载 作者:行者123 更新时间:2023-12-01 04:37:41 24 4
gpt4 key购买 nike

clj-http 解释了 how to set up logging with 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上给出了答案enter link description here -- 但是在这里重新发布它可能会很有用。

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

关于logging - 带有 logback 的 clj-http 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57233354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com