I am 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 and most of the problem is 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.

Any help is appreciated...this is driving me nuts.

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

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

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?

In the 4.2.1 source, the log names are: "org.apache.http.headers" and "org.apache.http.wire", though even using them, the noisy apache logging won't seem to shut off for me.
–
TinclonSep 12 '12 at 1:24

Thank you!!! Also: If you have this issue somewhere in your own code, where you use httpclient and don't already use Log4j: Also remember to include the log4j jar in the classpath ...
–
FelixDJan 22 '14 at 14:44

Note: Some of this answer might repeat things you already know (or think you know), but there is a bit of mis-information floating around on this question, so I'm going to start at the beginning and spell it all out

Commons HttpClient uses Commons-Logging for all its logging needs.

Commons-Logging is not a full logging framework, but rather, is a wrapper around several existing logging frameworks

That means that when you want to control the logging output, you (mostly) end up configuring a library other than Commons-Logging, but because Commons-Logging wraps around several other libraries, it's hard for us to guess which one to configure without knowing your exactly setup.

Commons-Logging can log to log4j, but it can also log to java.util.logging (JDK1.4 logging)

Commons-Logging tries to be smart and guess which logging framework you are already using, and send its logs to that.

If you don't already have a logging framework, and are running on a JRE that's 1.4 or above (which you really should be) then it will probably be sending its log messages to the JDK logging (java.util.logging)

Relying on Commons-Logging's autodiscovery mechanism is prone to error. Simply adding log4j.jar onto the classpath would cause it to switch which logging mechanism it uses, which probably isn't what you want

It is preferable for you to explicitly tell Commons-Logging which logging library to use

The steps you want to follow to configure the commons-httpclient logging are

Decide which underlying logging framework you want to use. There are a number of choices, but probably log4j or java.util.logging are the best options for you.

Set-up the commons-logging properties file to point to the correct Log implementation. e.g. to use log4j, put this into the properties file: org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger, or to use JDK logging set org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger. These can also be set as system properties (e.g. using -D on the command line).

Configure the underlying logging implementation (e.g. log4j) to ignore the messages you don't want, and output the messages you do want.

That's a lot of steps, but that's what it takes. The developers at Apache-commons tend to assume you'll already have a logging framework configured, and they can work out which one it is by auto-discovery.
If that's not true for you, then it tends to be a bit more work to get things running.

In your log4.properties - do you have this set like I do below and no other org.apache.http loggers set in the file?

-org.apache.commons.logging.simplelog.log.org.apache.http=ERROR

Also if you don't have any log level specified for org.apache.http in your log4j properties file then it will inherit the log4j.rootLogger level. So if you have log4j.rootLogger set to let's say ERROR and take out org.apache.http settings in your log4j.properties that should make it only log ERROR messages only by inheritance.

UPDATE:

Create a commons-logging.properties file and add the following line to it. Also make sure this file is in your CLASSPATH.

I didn't have a need for a log4j.properties file before trying to use HttpClient. I tried putting your lines in my src/test/resources/log4j.properties file but it didn't make any difference. Is it even right to put commons.logging options in log4j.properties?
–
Matt BakerFeb 6 '11 at 19:21

Yeah commons-logging is just a wrapper around log4j. How do you invoke the logger in your test class?
–
CoolBeansFeb 6 '11 at 19:25

(After your update) I made it so that the above line was the only one in my log4j.properties file and it still spits out everything.
–
Matt BakerFeb 6 '11 at 19:27

1

I don't invoke the logger, HttpClient does it on its own.
–
Matt BakerFeb 6 '11 at 19:27

It says on the link you provided that - "Note: Log4j is not included in the HttpClient distribution." So you definitely need to add it to your CLASSPATH. Are you seeing the output in stdout (console) or in a log file? I will create a sample log4h file with code to invoke it for you.
–
CoolBeansFeb 6 '11 at 19:33

Thanks, I had the same problem when using OpenRdf. All the other tips in this thread seemed to have no effect until I removed the logback jars, now the log is nicely quiet.
–
amarillionDec 19 '12 at 12:01

I am using log4j. Adding the first two lines in the log solver my problem completely. All my other log messages appear according to the level I set. Whereas apache logs doesn't. Great help. Thanks.
–
ArunTSOct 7 '14 at 11:43

I was led to this post when searching for solution for similar problem. Tim's answer was very helpful. like Matt Baker, I just want to shut off httpClient log without too much configuration. As we were not sure which logging implementation underneath common-logging was used, My solution was to force it using log4j by throwing log4j jar file in the class path. Default setting of log4j configuration shuts off common-httpclient debug output. Of course, to make it more robust, you may create common-logging.properties and log4j.properties files to further define your logging configurations.

I've been plagued by the same issue for quite some time now and finally decided to look into this. It turned out the issue is that my project had a dependency on http-builder-0.5.2.jar which bundled a log4j.xml file within itself. And sure enough, the log level for org.apache.http.wire was DEBUG! The way I found it was just to go through all the jar files in my dependencies and do "jar tvf" and gripping for log4j.

While this discovery led to the eventual solution of upping the version of my http-builder dependency to 0.6, it still baffles me what must have gone through the developer's mind when bundling the log4j.xml file into the jar file. Anyway, that's probably not relevant to this thread for now. But I figured it's useful to mention this solution I found given that when I was searching for a solution before now, mine never came up. Hopefully someone will find this useful.