如何在 Android 上为 apache commons HttpClient 启用日志记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3246792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable logging for apache commons HttpClient on Android
提问by alex2k8
To enable logging for apache commons HttpClient in normal Java application I used:
要在我使用的普通 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");
But on android I don't see logs in LogCat.
但是在 android 上我没有在 LogCat 中看到日志。
Am I missing some thing?
我错过了什么吗?
采纳答案by alex2k8
Here is a solution (without digging into details)
这是一个解决方案(无需深入研究)
Console:
安慰:
adb shell setprop log.tag.httpclient.wire.header VERBOSE
adb shell setprop log.tag.httpclient.wire.content VERBOSE
Code:
代码:
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);
Test:
测试:
java.util.logging.Logger.getLogger("httpclient.wire.content").log(java.util.logging.Level.CONFIG, "hola");
回答by Joe
Ignore my earlier comment. I found the solution on the org.apache.http logging page. Your original answer was referring to httpclient-3.x logging, and the working code for recent versions comes from http-components logging
忽略我之前的评论。我在 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");
and properties:
和属性:
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
The difference is in the logging tag names.
不同之处在于日志标记名称。
回答by Michael
The devil is in the details. I'm running the 2.3.3 emulator and got it working with:
细节决定成败。我正在运行 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);
and in the adb shell
并在 adb shell 中
# setprop log.tag.org.apache.http.wire VERBOSE
# setprop log.tag.org.apache.http.headers VERBOSE
Thus it seems the log specifiers are different.
因此,日志说明符似乎不同。
回答by nut
You just need to use
你只需要使用
java.util.logging.Logger.getLogger(yourFullClassName).setLevel(java.util.logging.Level.All);
and
和
adb shell setprop log.tag.correspondingTag VERBOSE
Android use this function to get correspondingTag from class full name:
Android 使用这个函数从类全名中获取对应的Tag:
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);
}
so for example,I want to enable logging for “org.apache.http.impl.client.DefaultRequestDirector” class,do such things below:
例如,我想为“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")
}
And then in adb shell
然后在 adb shell 中
setprop log.tag.DefaultRequestDirector VERBOSE