Disable logging of apache HTTP Client?

13,530

Solution 1

Assuming you are using httpclient 4, doesn't adding something like this to your log4j.xml work:

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

I you don't have a log4j.xml create one and it to your classpath.

If you're using httpclient 3 then you'll have to use something like:

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

Solution 2

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

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:

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. When it stops at line 56, read the log value. As it is an object of type Log, open it an look at its "name" property. You will see the name "httpclient". Obvioulsly, the Apache documentation is wrong.

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.

Share:
13,530
user381878
Author by

user381878

Updated on June 12, 2022

Comments

  • user381878
    user381878 almost 2 years

    I am writing an application in which I am uploading a file using HTTP protocol to a server. Everything is working fine and I am able to upload the file, I have used Apache HTTP client jar set to accomplish this. 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. Can anyone guide me how I can i disable logging of apache Http client?

    I am configuring log4j with the help of an XML file name log4j.xml.