简体   繁体   English

禁用apache HTTP Client的日志记录?

[英]Disable logging of apache HTTP Client?

I am writing an application in which I am uploading a file using HTTP protocol to a server. 我正在编写一个应用程序,我使用HTTP协议将文件上传到服务器。 Everything is working fine and I am able to upload the file, I have used Apache HTTP client jar set to accomplish this. 一切都工作正常,我能够上传文件,我已经使用Apache HTTP客户端jar设置来实现这一目标。 In the application I have used log4j logging framework which has been set to DEBUG level, by default Apache HTTP Client has also picked up the same logging framework with same logging level and it is producing tons of logs. 在应用程序中,我使用了已设置为DEBUG级别的log4j日志框架,默认情况下,Apache HTTP Client也选择了具有相同日志记录级别的相同日志框架,并且它生成了大量日志。 Can anyone guide me how I can i disable logging of apache Http client? 任何人都可以指导我如何禁用apache Http客户端的日志记录?

I am configuring log4j with the help of an XML file name log4j.xml. 我正在使用XML文件名log4j.xml配置log4j。

Assuming you are using httpclient 4 , doesn't adding something like this to your log4j.xml work: 假设你正在使用的HttpClient 4,不添加类似这样你的log4j.xml工作:

<logger name="org.apache.http">
  <level value="warn"/> 
</logger>

I you don't have a log4j.xml create one and it to your classpath. 我没有log4j.xml创建一个它和你的类路径。

If you're using httpclient 3 then you'll have to use something like: 如果您使用的是httpclient 3,那么您将不得不使用以下内容:

<logger name="org.apache.commons.httpclient">
  <level value="warn"/> 
</logger>

In these examples I've set the level to warn , you might chose to use none , but a minimum of error would be sensible. 在这些例子中,我设置了警告级别,你可能选择使用none ,但是最小的错误是明智的。

The given answers are good examples of people answering questions they don't even understand. 给出的答案是人们回答他们甚至不理解的问题的好例子。 They just repeat what they have heard or read in poor documentations like the one from Apache HTTP client. 他们只是重复他们在Apache HTTP客户端的糟糕文档中听到或读过的内容。 Something like: 就像是:

<logger name="org.apache.commons.httpclient">
  <level value="warn"/> 
</logger>

is what should be done if the documentation of Apache HTTP client was right. 如果Apache HTTP客户端的文档是正确的,应该怎么做。 In such case, the poster of this question would have resolved the problem himself. 在这种情况下,这个问题的海报本身就能解决问题。 Giving such poor answers is kind of insulting. 给出这么糟糕的答案有点侮辱。

The problem is that although the documentation says: 问题是虽然文档说:

Each class has its own log named according to the class's fully qualified name. 每个类都有自己的日志,根据类的完全限定名称命名。 For example the class HttpClient has a log named org.apache.commons.httpclient.HttpClient. 例如,类HttpClient有一个名为org.apache.commons.httpclient.HttpClient的日志。 Since all classes follow this convention it is possible to configure context logging for all classes using the single log named org.apache.commons.httpclient. 由于所有类都遵循此约定,因此可以使用名为org.apache.commons.httpclient的单个日志为所有类配置上下文日志记录。

it is simply not true. 这根本不是真的。

Better than giving the answer is showing how to find it yourself. 比给出答案更好的是展示如何自己找到它。 To find the solution, you have first to enable location in the log to see where the log happens. 要找到解决方案,首先要在日志中启用位置以查看日志发生的位置。 Then the logs will displays a line like: 然后日志将显示如下行:

2013-02-07 15:33:02,369 DEBUG [Some thread name] org.apache.commons.httpclient.Wire.wire(Wire.java:84) - << "[\r]"

This shows that logging is happening in class Wire.java, line 84. You may then open the sources in your favorite IDE and and you will see the following: 这表明日志记录发生在第84行的Wire.java类中。然后,您可以在您喜欢的IDE中打开源代码,您将看到以下内容:

80    if (buffer.length() > 0) {
81        buffer.append("\"");
82        buffer.insert(0, "\"");
83        buffer.insert(0, header);
84        log.debug(buffer.toString()); // Log is happening here
85    }

The log variable holds the logger. 日志变量保存记录器。 Go to the place where this variables receives its value: 转到此变量接收其值的位置:

52 /** Log for any wire messages. */
53 private Log log;
54 
55 private Wire(Log log) {
56    this.log = log;
57 }

All you have to do now is to put a break point at line 56 and run your application in the debugger. 您现在要做的就是在第56行设置一个断点并在调试器中运行您的应用程序。 When it stops at line 56, read the log value. 当它在第56行停止时,读取日志值。 As it is an object of type Log, open it an look at its "name" property. 因为它是Log类型的对象,所以打开它看看它的“name”属性。 You will see the name "httpclient". 您将看到名称“httpclient”。 Obvioulsly, the Apache documentation is wrong. 显然,Apache文档是错误的。

Now you can disable this logger with: 现在您可以使用以下命令禁用此记录器:

<logger name="httpclient">
  <level value="warn"/> 
</logger>

The conclusion is: 结论是:

  • When possible, learn to find answer to your own question instead of asking. 在可能的情况下,学会找到自己问题的答案而不是询问。

  • Do not believe what everybody says. 不要相信每个人都说的话。 Naming the loggers by the fully qualified class name is a convention. 使用完全限定的类名命名记录器是一种约定。 Like all conventions, nobody is obliged to follow it. 像所有公约一样,没有人有义务遵守它。 An Apache do not. 阿帕奇没有。

  • Do not answer question when you don't know the answer. 当你不知道答案时,不要回答问题。 This is only noise. 这只是噪音。

You can adjust logging level per package. 您可以调整每个包的日志记录级别。 Here is example from http://wiki.apache.org/logging-log4j/Log4jXmlFormat : 以下是http://wiki.apache.org/logging-log4j/Log4jXmlFormat的示例:

<logger name="com.eatmutton.muttonsite.torque" additivity="false">
   <level value="info" />
   <appender-ref ref="local-torque" />
</logger>

So, even if your default level is "debug", for classes of com.eatmutton.muttonsite.torque package (and nested packages) the level would be "info" So, you need to find out package to which HTTP client classes belong and add such record to your config. 因此,即使您的默认级别为“debug”,对于com.eatmutton.muttonsite.torque包(和嵌套包)的类,级别也将为“info”。因此,您需要查找HTTP客户端类所属的包和将此记录添加到您的配置中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM