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

时间:2014-02-04 13:39:17

标签: java apache logging log4j2 apache-httpclient-4.x

我试图激活我的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的警告级别从调整更改为调试非常有效。

这些库使用:

<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 个答案:

答案 0 :(得分: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

答案 1 :(得分:3)

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页面详细解释了如何配置maven依赖项以包含桥库:

  

Log4j 1.x API Bridge

     

如果现有组件使用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>

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