禁用 HttpClient 日志记录

编程入门 行业动态 更新时间:2024-10-25 00:27:24
本文介绍了禁用 HttpClient 日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在集成测试套件中使用 commons-httpclient 3.1.HttpClient 的默认日志记录非常嘈杂,我似乎无法将其关闭.我试过按照这里的说明进行操作,但没有一个区别.

大多数情况下,我只需要关闭 org.apache.http.wire 记录器.部分问题是我不知道 HttpClient 尝试使用什么类型的记录器.我以前从未使用过这个库.我尝试创建一个 log4j.properties 文件并将其放入我的 test/resources 文件夹中,修改 jre/lib 中的主 logging.properties 文件,并按照 日志页面,它们都没有任何区别.

更新: 更正:似乎有问题的输出实际上源自 jwebunit 对 HttpClient 的使用,而不是我自己的.无论哪种方式,都是不可取的.

更新:感谢您到目前为止的尝试.我已经尝试了下面建议的所有内容,但仍然没有运气.我的 src/test/resources 文件夹中有一个文件 commons-logging.properties,其中包含以下内容

org.apachemons.logging.LogFactory=org.apachemons.logging.impl.Log4jFactorylog4j.configuration=log4j.properties

和一个文件 log4j.properties 在同一个文件夹中,内容如下

log4j.rootLogger=错误,标准输出log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n#这是应该让httpclient关闭的行log4j.logger.apache.http=错误

然而,当我运行我的测试时,我仍然得到一堆这样的输出:

21:57:41.413 [main] DEBUG org.apache.http.wire - <<"[][ ]"21:57:41.413 [main] 调试 org.apache.http.wire - <<[][ ]"21:57:41.413 [main] 调试 org.apache.http.wire - <<"[][ ]"21:57:41.413 [main] 调试 org.apache.http.wire - <<"</ul>[ ]";21:57:41.413 [main] 调试 org.apache.http.wire - <<"[ ]"21:57:41.424 [主要] 调试 org.apache.http.wire - <<[ ]"21:57:41.425 [main] 调试 org.apache.http.wire - <<[][ ]"21:57:41.425 [main] 调试 org.apache.http.wire - <<[][ ]"21:57:41.425 [main] 调试 org.apache.http.wire - <<"</div>[][ ]"21:57:41.425 [main] 调试 org.apache.http.wire - <<"</li>[][ ]"21:57:41.425 [main] 调试 org.apache.http.wire - <<"[][ ]"21:57:41.425 [main] 调试 org.apache.http.wire - <<"[][ ]"21:57:41.433 [主要] 调试 org.apache.http.wire - <<"</ul>[ ]";21:57:41.433 [主要] 调试 org.apache.http.wire - <<</div>[ ]"21:57:41.433 [主要] 调试 org.apache.http.wire - <<[ ]"21:57:41.433 [主要] 调试 org.apache.http.wire - <<</div>[ ]"21:57:41.433 [主要] 调试 org.apache.http.wire - <<[ ]"21:57:41.433 [主要] 调试 org.apache.http.wire - <<[ ]"21:57:41.433 [主要] 调试 org.apache.http.wire - <<[ ]"21:57:41.433 [主要] 调试 org.apache.http.wire - <<[ ]"21:57:41.433 [主要] 调试 org.apache.http.wire - <<<div class="details">[ ]";21:57:41.442 [main] 调试 org.apache.http.wire - <<[ ]"21:57:41.443 [main] 调试 org.apache.http.wire - <<[ ]"21:57:41.443 [main] 调试 org.apache.http.wire - <<"<div class="details-body details-precis ">[ ]"21:57:41.443 [main] 调试 org.apache.http.wire - <<<div class="details-state">[ ]";21:57:41.443 [main] 调试 org.apache.http.wire - <<[ ]"21:57:41.443 [main] 调试 org.apache.http.wire - <<</div>[ ]"21:57:41.443 [main] 调试 org.apache.http.wire - <<</div>[ ]"21:57:41.443 [main] 调试 org.apache.http.wire - <<[ ]"21:57:41.455 [主要] 调试 org.apache.http.wire - <<[ ]"21:57:41.455 [主要] 调试 org.apache.http.wire - <<</div>[ ]"21:57:41.455 [主要] 调试 org.apache.http.wire - <<[ ]"21:57:41.455 [主要] 调试 org.apache.http.wire - <<</div>[ ]"21:57:41.455 [主要] 调试 org.apache.http.wire - <<</div>[ ]"21:57:41.455 [主要] 调试 org.apache.http.wire - <<[ ]"21:57:41.455 [主要] 调试 org.apache.http.wire - <<[ ]"21:57:41.455 [主要] 调试 org.apache.http.wire - <<[ ]"21:57:41.455 [主要] 调试 org.apache.http.wire - <<[][ ]"销毁 1 个进程 21:57:41.465 [main] DEBUG org.apache.http.wire - <<[][ ]"

这个针对所有线路上的输出使我无法使用这个库……直到我想出如何将其关闭为止.我需要做什么特别的事情来读取这个日志配置吗?

解决方案

更新 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.apachemons.httpclient 使用Log4j 将日志级别设置为WARN 将无法按预期工作:

log4j.logger.apachemons.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.apachemons.logging.LogFactory=org.apachemons.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.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.apachemons.httpclient in log4j.properties will not work as expected:

log4j.logger.apachemons.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"));

更多推荐

禁用 HttpClient 日志记录

本文发布于:2023-11-24 04:18:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623939.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日志   HttpClient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!