为Log4J2 + Apache HttpClient启用调试日志记录

Ste*_*anM 8 java apache logging apache-httpclient-4.x log4j2

我试图激活我的Apache HttpClient的调试日志记录,但无法使其工作(没有得到任何与HttpClient相关的日志记录输出).

这是我目前使用的log4j2配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
    <appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
        </Console>

        <RollingFile name="RollingRandomAccessFile" fileName="logs/test.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>
                    %d %p %c{1.} [%t] %m%n
                </Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy max="20" />
        </RollingFile>

        <Async name="async">
            <AppenderRef ref="RollingRandomAccessFile" />
        </Async>
    </appenders>
    <loggers>
        <logger name="org.apache.http.wire" level="debug" />
        <logger name="org.apache.http.client" level="debug" />
        <logger name="org.apache.xerces.parsers.SAXParser" level="warn" />
        <logger name="org.hibernate" level="warn" />
        <root level="trace">
            <appender-ref ref="console" />
            <appender-ref ref="async" />
        </root>
    </loggers>
</configuration>
Run Code Online (Sandbox Code Playgroud)

例如,将hibernate的警告级别从警告更改为调试非常有效.

这些库我使用:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>fluent-hc</artifactId>
    <version>4.2.6</version>
    </dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2.6</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.2.5</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient-cache</artifactId>
    <version>4.2.6</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.2.6</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

Log4J2

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0-beta9</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?我尝试过不同的包名,比如

httpclient.wire
httpclient.wire.header
httpclient.wire.content
Run Code Online (Sandbox Code Playgroud)

还有一些但似乎没什么用......

Rem*_*pma 9

我假设httpcomponents在内部使用log4j-1.2.Log4j2提供了一个适配器,用于将使用1.2 API的应用程序的调用路由到新的2.0实现.

要启用此功能,您只需将log4j-core和log4j-1.2-api jar添加到类路径中.(您当前的Maven依赖项只有log4j-api,这是新的2.0 API.)另请参阅http://logging.apache.org/log4j/2.x/faq.html.


小智 5

Remko 是正确的,问题是 httpcomponents 在内部使用 log4j-1.2。将 log4j 1.2 路由到 log4j 2 的适配器称为 log4j 1.2 桥接器,其描述如下:https : //logging.apache.org/log4j/2.x/log4j-1.2-api/index.html

log4j 的 maven artifacts page 解释了如何配置你的 maven 依赖以包含桥库:

Log4j 1.x API 桥接器

如果现有组件使用 Log4j 1.x 并且您希望将此日志记录路由到 Log4j 2,则删除所有 log4j 1.x 依赖项并添加以下内容。

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.5</version>
  </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

来自https://logging.apache.org/log4j/2.x/maven-artifacts.html