且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

禁用 HttpClient 日志记录

更新时间:2022-10-15 17:29:35

更新 log4j.properties 以包括:

log4j.logger.httpclient.wire.header=WARNlog4j.logger.httpclient.wire.content=警告

请注意,如果未安装 Log4j 库,HttpClient(因此 JWebUnit)将使用 logback.在这种情况下,创建或编辑 logback.xml 以包括:

<logger name="org.apache" level="WARN"/><logger name="httpclient" level="WARN"/></配置>

使用log4j.properties 中的包名org.apache.commons.httpclient 使用Log4j 将日志级别设置为WARN 将无法按预期工作:

log4j.logger.org.apache.commons.httpclient=WARN

这是因为 HttpClient (v3.1) 的源使用以下日志名称:

public static Wire HEADER_WIRE = new Wire(LogFactory.getLog("httpclient.wire.header"));public static Wire CONTENT_WIRE = new Wire(LogFactory.getLog("httpclient.wire.content"));

I´m using commons-httpclient 3.1 in an integration test suite. The default logging for HttpClient is extremely noisy and I can't seem to turn it off. I've tried following the instructions here but none of them make any difference.

Mostly I just need to make the org.apache.http.wire logger shut up. Part of the problem is that I don't know what type of logger HttpClient is trying to use. I've never used this library before. I tried creating a log4j.properties file and dropping it in my test/resources folder, modifying the master logging.properties file in jre/lib, and sending in the various logging options to Maven as specified on the logging page, and none of them make any difference.

UPDATE: A correction: it appears the output in question is actually originating through jwebunit's usage of HttpClient, not my own. Either way, it's not desirable.

UPDATE: Thanks for the attempts so far. I've tried everything suggested below but still no luck. I have a file commons-logging.properties in my src/test/resources folder with the following contents

org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.Log4jFactory
log4j.configuration=log4j.properties

and a file log4j.properties in the same folder with the following contents

log4j.rootLogger=ERROR, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n

#This is the line that should make httpclient shut up
log4j.logger.org.apache.http=ERROR

However, when I run my tests I still get a bunch of output like this:

21:57:41.413 [main] DEBUG org.apache.http.wire - << "                                   [
][
]"
21:57:41.413 [main] DEBUG org.apache.http.wire - << "[
][
]"
21:57:41.413 [main] DEBUG org.apache.http.wire - << "                                   [
][
]"
21:57:41.413 [main] DEBUG org.apache.http.wire - << "                               </ul>[
]"
21:57:41.413 [main] DEBUG org.apache.http.wire - << "    [
]"
21:57:41.424 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.425 [main] DEBUG org.apache.http.wire - << "[
][
]"
21:57:41.425 [main] DEBUG org.apache.http.wire - << "[
][
]"
21:57:41.425 [main] DEBUG org.apache.http.wire - << "                   </div>[
][
]"
21:57:41.425 [main] DEBUG org.apache.http.wire - << "                </li>[
][
]"
21:57:41.425 [main] DEBUG org.apache.http.wire - << "            [
][
]"
21:57:41.425 [main] DEBUG org.apache.http.wire - << "            [
][
]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "        </ul>[
]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "</div>[
]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "</div>[
]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.433 [main] DEBUG org.apache.http.wire - << "<div class="details">[
]"
21:57:41.442 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "<div class="details-body details-precis  ">[
]
"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "<div class="details-state">[
]"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "</div>[
]"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "</div>[
]"
21:57:41.443 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "</div>[
]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "</div>[
]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "</div>[
]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "[
]"
21:57:41.455 [main] DEBUG org.apache.http.wire - << "[
][
]"
Destroying 1 processes21:57:41.465 [main] DEBUG org.apache.http.wire - << "[
][
]"

This output for everything that comes across the wire is making this library unusable for me...that is until I can figure out how to turn it off. Is there anything special I need to do to get this log configuration read in?

Update log4j.properties to include:

log4j.logger.httpclient.wire.header=WARN
log4j.logger.httpclient.wire.content=WARN


Note that if Log4j library is not installed, HttpClient (and therefore JWebUnit) will use logback. In this situation, create or edit logback.xml to include:

<configuration>
    <logger name="org.apache" level="WARN" />
    <logger name="httpclient" level="WARN" /> 
</configuration>


Setting the log level to WARN with Log4j using the package name org.apache.commons.httpclient in log4j.properties will not work as expected:

log4j.logger.org.apache.commons.httpclient=WARN

This is because the source for HttpClient (v3.1) uses the following log names:

public static Wire HEADER_WIRE = new Wire(LogFactory.getLog("httpclient.wire.header"));
public static Wire CONTENT_WIRE = new Wire(LogFactory.getLog("httpclient.wire.content"));