无法使用 apache httpComponent 和 log4j2 输出日志

Cannot output log with apache httpComponent and log4j2

我想在控制台中输出 httpComponent 从 Apache 发送的请求。我在 log4j2.xml 中尝试了几个配置,但从未在控制台中得到与 http 相关的信息。我只看到我的日志应用程序。

这是我的 log4j2.xml 文件

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

        <logger name="org.apache.http" level="debug"/>
        <logger name="org.apache.wire" level="debug"/>
    </Loggers>
</Configuration>

这是我的小代码

public ApiResponse<DealDiscovery> execute() throws IOException {
        HttpClient client = HttpClientBuilder.create().build();
        URI uri = null;
        try {
            uri = new URIBuilder(BASE_URL).addParameter(TOKEN, token).build();
        } catch (URISyntaxException e) {
            //Should never happened
        }
        HttpGet http = new HttpGet(uri);
        HttpResponse httpResponse = client.execute(http);
        String response = EntityUtils.toString(httpResponse.getEntity());
        return parseResponse(response);
    }

这是我在 pom.xml

中的依赖关系

    <dependency>
        <groupId>com.saucelabs</groupId>
        <artifactId>sauce_junit</artifactId>
        <version>2.1.20</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>${selenium.version}</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.6.2</version>
    </dependency>

    <dependency>
        <groupId>org.skyscreamer</groupId>
        <artifactId>jsonassert</artifactId>
        <version>1.2.3</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-junit</artifactId>
        <version>2.0.0.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>19.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.5</version>
    </dependency>

    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.8.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.13</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.13</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient-cache</artifactId>
        <version>4.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.2</version>
    </dependency>

</dependencies>

我错过了什么?

答案是从这里复制的: 你需要在 pom.xml

添加如下依赖
<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.5</version>
  </dependency>
</dependencies>

此HttpClient使用旧log4j 1.2的原因api:https://logging.apache.org/log4j/2.x/maven-artifacts.html