put log4j logging httpclient

[亡魂溺海] 提交于 2019-12-14 04:26:23

问题


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.



来源:https://stackoverflow.com/questions/14459360/put-log4j-logging-httpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!