如何在Android上启用Apache公共HttpClient日志logging

为了在日常的Java应用程序中启用apache公共日志HttpClient我使用了:

System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug"); System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug"); 

但在Android上我看不到LogCat中的日志。

我错过了什么?

忽略我以前的评论。 我在org.apache.http日志页面上find了解决scheme。 您的原始答案是指httpclient-3.x日志logging ,最近版本的工作代码来自http-components日志logging

 java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST); java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINEST); System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug"); System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug"); System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "debug"); 

和属性:

 adb shell setprop log.tag.org.apache.http VERBOSE adb shell setprop log.tag.org.apache.http.wire VERBOSE adb shell setprop log.tag.org.apache.http.headers VERBOSE 

区别在于日志标记名称。

这是一个解决scheme(没有深入细节)

安慰:

 adb shell setprop log.tag.httpclient.wire.header VERBOSE adb shell setprop log.tag.httpclient.wire.content VERBOSE 

码:

 java.util.logging.Logger.getLogger("httpclient.wire.header").setLevel(java.util.logging.Level.FINEST); java.util.logging.Logger.getLogger("httpclient.wire.content").setLevel(java.util.logging.Level.FINEST); 

testing:

 java.util.logging.Logger.getLogger("httpclient.wire.content").log(java.util.logging.Level.CONFIG, "hola"); 

细节决定成败。 我正在运行2.3.3模拟器,并使其工作:

 java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST); java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINEST); 

并在adbshell

 # setprop log.tag.org.apache.http.wire VERBOSE # setprop log.tag.org.apache.http.headers VERBOSE 

因此看起来这些日志说明符是不同的。

你只需要使用

 java.util.logging.Logger.getLogger(yourFullClassName).setLevel(java.util.logging.Level.All); 

 adb shell setprop log.tag.correspondingTag VERBOSE 

Android使用这个函数从类全名得到对应的标签:

 public static String loggerNameToTag(String loggerName) { if (loggerName == null) { return "null"; } int length = loggerName.length(); if (length <= 23) { return loggerName; } int lastPeriod = loggerName.lastIndexOf("."); return length - (lastPeriod + 1) <= 23 ? loggerName.substring(lastPeriod + 1) : loggerName.substring(loggerName.length() - 23); } 

所以例如,我想启用“org.apache.http.impl.client.DefaultRequestDirector”类的日志logging,在下面做这样的事情:

 String clzName = "org.apache.http.impl.client.DefaultRequestDirector"; String newClzName = loggerNameToTag(clzName); System.out.println("className:" + clzName + " tagName is " + newClzName); //get tagName from class full name,and then it will be used in setprop Logger jdkLogger = Logger.getLogger(clzName); jdkLogger.setLevel(Level.ALL); if (jdkLogger.isLoggable(Level.FINE)) { jdkLogger.log(Level.FINE, "jdk log msg"); jdkLogger.log(Level.Fine,"tagName is") } 

然后在adb shell中

 setprop log.tag.DefaultRequestDirector VERBOSE