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

10

我正在尝试激活我的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>

对于hibernate来说,将日志级别从warn改为debug是完全可行的。

我正在使用以下这些库:

<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>

Log4J2

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0-beta9</version>
</dependency>

有人有想法吗?我已经尝试了不同的包名,例如:
httpclient.wire
httpclient.wire.header
httpclient.wire.content

还有一些其他的尝试,但似乎都不起作用...

2个回答

12
我假设httpcomponents内部使用log4j-1.2。 Log4j2提供了一个适配器,将使用1.2 API的应用程序调用路由到新的2.0实现。
要启用此功能,您只需要将log4j-core和log4j-1.2-api jars添加到类路径中。(您当前的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构件页面解释了如何配置您的maven依赖项以包括bridge库:

Log4j 1.x API Bridge

If existing components use Log4j 1.x and you want to have this logging routed to Log4j 2, then remove any log4j 1.x dependencies and add the following.

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.5</version>
  </dependency>
</dependencies>

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

该页面列出了Apache Log4j 2.x的Maven依赖项。您可以使用这些依赖项在项目中使用Log4j 2.x,并确保与其他库相容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接