在我使用的普通Java应用程序中启用apache commons 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中的日志。
我错过了什么吗?
答案 0 :(得分:61)
忽略我之前的评论。我在org.apache.http日志页面上找到了解决方案。您的原始答案是指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
区别在于日志标记名称。
答案 1 :(得分:8)
这是一个解决方案(没有深入细节)
<强>控制台:强>
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);
<强>测试强>
java.util.logging.Logger.getLogger("httpclient.wire.content").log(java.util.logging.Level.CONFIG, "hola");
答案 2 :(得分:3)
魔鬼在于细节。我正在运行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);
并在adb shell中
# setprop log.tag.org.apache.http.wire VERBOSE
# setprop log.tag.org.apache.http.headers VERBOSE
因此,日志说明符似乎不同。
答案 3 :(得分:3)
你只需要使用
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”类启用日志记录,请执行以下操作:
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