【问题标题】:Enable debug logging for Log4J2 + Apache HttpClient为 Log4J2 + Apache HttpClient 启用调试日志记录
【发布时间】:2014-02-28 12:26:09
【问题描述】:

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

例如,将休眠级别从警告更改为调试非常有效。

我正在使用这些库:

<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

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

【问题讨论】:

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


    【解决方案1】:

    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 依赖项以包含桥接库:

    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>
    

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

    【讨论】:

      【解决方案2】:

      我假设 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

      【讨论】:

        猜你喜欢
        • 2011-03-15
        • 2020-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-19
        • 1970-01-01
        相关资源
        最近更新 更多