Athafoud
Athafoud

Reputation: 3000

Set apache HttpClient log level explicitly

I am developing a web application which among the other uses the apache HttpClient to make some httpRequest.
For logging I am using slf4j with the slf4j-log4j12 'plug in'

What I want is to have DEBUG log level for my application but WARN level for the HttpClient. I am seeting the logging properties in log4j.properties.

log4j.rootLogger=DEBUG,console,file

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout   
log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.appender.file=org.apache.log4j.RollingFileAppender   
log4j.appender.file.File=log.log   
log4j.appender.file.threshold=DEBUG

The initialization and logging is like that

import org.slf4j.LoggerFactory;
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(<MY_CLASS>.class.getName());

LOG.debug("This is debug info");
LOG.warn("This is warn info");

Until now I am setting the following to the class that uses HttpClient

System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

In order to 'deactivate' logging, but this stops the logging completely and does not set the level to WARN as I want.

Until now I have tried what is proposed on the following SO questions but with no luck. I have also seen many other which suggest pretty much the same.

Passing parameters on execution such as java -Dlog4j is not a solution for me.

As a sidenote my project has two modules,

Upvotes: 4

Views: 4786

Answers (1)

palimpsestor
palimpsestor

Reputation: 1069

Try adding these three lines to your log4j.properties file:

log4j.logger.org.apache.http=WARN
log4j.logger.org.apache.http.headers=WARN
log4j.logger.org.apache.http.wire=WARN

Upvotes: 2

Related Questions