为 Log4J2 + Apache HttpClient 启用调试日志记录 [英] Enable debug logging for Log4J2 + Apache HttpClient

查看:36
本文介绍了为 Log4J2 + Apache HttpClient 启用调试日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的 Apache HttpClient 激活调试日志记录,但无法使其工作(根本没有获得与 HttpClient 相关的日志记录输出).

im trying to activate the debug logging for my Apache HttpClient but cant make it work (getting no logging output at all which is HttpClient related).

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

This is my log4j2 configuration im using at the moment:

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

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

Changing the level from warn to debug for hibernate works perfectly for example.

我使用的这些库:

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

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

Does anybody have a idea? I tried already different package names like

httpclient.wire
httpclient.wire.header
httpclient.wire.content

还有一些,但似乎没有任何效果...

and some more but nothing seems to work...

推荐答案

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

I am assuming that httpcomponents use log4j-1.2 internally. Log4j2 provides an adapter that routes calls from your application that use the 1.2 API to the new 2.0 implementation.

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

To enable this, you only need to add log4j-core and log4j-1.2-api jars to your classpath. (Your current Maven dependencies only has log4j-api, which is the new 2.0 API.) See also http://logging.apache.org/log4j/2.x/faq.html .

这篇关于为 Log4J2 + Apache HttpClient 启用调试日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆