如何配置 Apache HttpClient 记录器?

问题描述 投票:0回答:1

我正在使用

RestAssured 5.3.0
,我想记录低级别的 http 请求/响应。
RequestLoggingFilter
文档说:

...如果您需要记录线路上实际发送的内容,请参阅 HTTP 客户端日志记录文档或使用外部工具,例如 Wireshark 。

因此,我直接转到他们指向的链接:https://hc.apache.org/httpcomponents-client-5.2.x/logging.html

该链接是关于

HttpClient v5
,但
RestAssured 5.3.0
使用了
v4.5
。这可以从
mvn dependency:tree
看出:

[INFO] +- io.rest-assured:rest-assured:jar:5.3.0:compile
[INFO] |  +- org.apache.groovy:groovy:jar:4.0.6:compile
[INFO] |  +- org.apache.groovy:groovy-xml:jar:4.0.6:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.5.13:compile

所以,我实际上应该查看这个链接:https://hc.apache.org/httpcomponents-client-4.5.x/logging.html

根据我在指南中的理解,我只需在我的

log4j2.xml
中添加一个记录器,http 客户端就应该开始生成日志。 这是我在指南中看到的示例。该部分称为
Enable full wire + context logging
:

<Configuration>
  <Appenders>
    <Console name="Console">
      <PatternLayout pattern="%d %-5level [%logger] %msg%n%xThrowable" />
    </Console>
  </Appenders>
  <Loggers>
    <Logger name="org.apache.http" level="DEBUG">
      <AppenderRef ref="Console"/>
    </Logger>
    <Root level="INFO">
      <AppenderRef ref="Console" />
    </Root>
  </Loggers>
</Configuration>

这是我的

log4j2.xml
。请注意那里的评论:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" strict="true">
    <Appenders>
        <Appender type="Console" name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{3} - %msg%n"/>
        </Appender>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console"/>
        </Root>

       <Logger name="org.apache.http" level="all">
            <AppenderRef ref="Console"/>
       </Logger>

        <!-- desperate attempt to enable the logging on even higher hierarchy level-->
        <Logger name="org.apache" level="all">
            <AppenderRef ref="Console"/>
        </Logger>

    </Loggers>
</Configuration>

这是我的

pom.xml
与日志依赖项相关的部分:

...
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.21.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j2-impl -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j2-impl</artifactId>
            <version>2.21.1</version>
            <scope>test</scope>
        </dependency>
...

我运行了测试,但

org.apache.*
记录器的日志记录没有打印到控制台。

我做错了什么?

编辑:添加 MVP 示例

RestAssured pom 依赖:

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>5.3.0</version>
        </dependency>

代码:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.Test;

public class TestClass {
    Logger logger = LogManager.getLogger(this);

    @Test
    public void test() {
        logger.info("Before request");

        RestAssured.given()
                .get("https://google.com");

        logger.info("After request");
    }
}

输出:

22:33:51.329 [main] INFO  TestClass - Before request
22:33:52.769 [main] INFO  TestClass - After request

===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0
java log4j2 rest-assured apache-httpcomponents
1个回答
0
投票

您的

Console
附加程序配置不正确;它必须是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" strict="true">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{3} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="org.apache.http.wire" level="DEBUG"/>
        <Root level="INFO">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

控制台Appender

下一个问题是

org.apache.httpcomponents:httpclient:jar:4.5.13
使用
org.apache.commons.logging.*
类进行日志记录,而不是
slf4
:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

...

private final Log headerLog = LogFactory.getLog("org.apache.http.headers");
private final Log wireLog = LogFactory.getLog("org.apache.http.wire");

要查看日志,您需要添加 Commons Logging Bridge 依赖项:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-jcl</artifactId>
    <version>2.12.1</version>
</dependency>

添加此

<Logger name="org.apache.http.wire" level="DEBUG"/>
后,您将看到日志:

[main] INFO  org.example.AppTest - Before request
[main] DEBUG apache.http.wire -  >> "GET / HTTP/1.1[\r][\n]"
[main] DEBUG apache.http.wire -  >> "Accept: */*[\r][\n]"
[main] DEBUG apache.http.wire -  >> "Host: google.com[\r][\n]"
[main] DEBUG apache.http.wire -  >> "Connection: Keep-Alive[\r][\n]"
[main] DEBUG apache.http.wire -  >> "User-Agent: Apache-HttpClient/4.5.13 (Java/21)[\r][\n]"
[main] DEBUG apache.http.wire -  >> "Accept-Encoding: gzip,deflate[\r][\n]"
[main] DEBUG apache.http.wire -  >> "[\r][\n]"
[main] DEBUG apache.http.wire -  << "HTTP/1.1 301 Moved Permanently[\r][\n]"
[main] DEBUG apache.http.wire -  << "Location: https://www.google.com/[\r][\n]"
© www.soinside.com 2019 - 2024. All rights reserved.