put log4j logging httpclient

2019-09-05 09:10发布

问题:

I want to put on logging in httpclient with log4j. Doing this, I added a file commons-logging.properties with the content org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger. Then I added a file log4j.properties with the following content provided by httpclient documentation as a logging example:

log4j.rootLogger=INFO, 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

log4j.logger.org.apache.http=DEBUG
log4j.logger.org.apache.http.wire=ERROR

Both files reside in the classpath. I put the following parameters

-Dlog4j.configuration=log4j.properties
-Dlog4j.debug

However, when I run my programm which executes http requests with the httpclient I do not see any logging output. The latter parameter should make the output of log4j verbose in that sense, that it displays several things e.g. the used properties files. This is not displayed at all. What I tried as wel is to read out the logger programmaciatally with

Logger.getLogger("log4j.logger.org.apache.http.wire").getLevel().toString()

this throws an null pointer exception.

Additionally I read

LogManager.getCurrentLoggers().hasMoreElements()

this is always false.

It seems like log4j is not "activated" for httpclient. Can anyone help me here what I could do in order to turn the logging on?

(Yes, I already read related topics here at stackoverflow)

Thank you very much

Felix

回答1:

there is some kind of wierd conflict involving the commons log interface and the CW on how to active/deactive the WIRE and HEADER loggers when using httpclient on android...

concerning just one project that chose to overload/reimplement android logger, providing a hook for the WIRE and HEADER loggers on android, check here

FWIW if you are doing lots of low level usage in android of REST interfaces and u just want quick way to toggle logging, I have had luck with his project and i just use 2 versions of a jar , depending on whether i need logging. Each new rev on native, apache core or client, you can run his script to convert apache native for android usage. Its a bit down in the weeds but it will work.