如何在 Android 上启用 apache commons HttpClient 的日志记录
为了在普通 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 中看不到日志。
我错过了什么吗?
To enable logging for apache commons HttpClient in normal Java application I used:
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.
Am I missing some thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
忽略我之前的评论。我在 org.apache.http 日志记录页面上找到了解决方案。您原来的答案是指 httpclient-3.x 日志记录,最近版本的工作代码来自 http 组件日志记录
和属性:
区别在于日志记录标记名称。
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
and properties:
The difference is in the logging tag names.
这是一个解决方案(无需深入研究细节)
控制台:
代码:
测试:
Here is a solution (without digging into details)
Console:
Code:
Test:
魔鬼在于细节。我正在运行 2.3.3 模拟器并让它与:
和 adb shell
一起工作因此,日志说明符似乎是不同的。
The devil is in the details. I'm running the 2.3.3 emulator and got it working with:
and in the adb shell
Thus it seems the log specifiers are different.
你只需要使用
Android
使用这个函数从类全名中获取相应的Tag:
例如,我想为“org.apache.http.impl.client.DefaultRequestDirector”类启用日志记录,请执行以下操作
:在亚行外壳中
You just need to use
and
Android use this function to get correspondingTag from class full name:
so for example,I want to enable logging for “org.apache.http.impl.client.DefaultRequestDirector” class,do such things below:
And then in adb shell
对于 Apache Http Components 版本 5.x,您需要使用
slf4j
logging Facade,例如JUL
(Jakarta Common Logging) 在编译时绑定日志记录后端。通过检索
root
记录器或
将日志记录级别设置为适当的级别,否则消息将被丢弃
实现您自己的
java.util.logging.Handler
以接收消息设置处理程序
并为为模块提供了一个
setLevel
函数。容器需要对未初始化的外部记录器保持强引用,否则垃圾收集器将取消分配,并且level
参数将设置为默认值。保存
modules
和函数的
类并像这样使用它(我将所有内容封装在
Logging
类中)当请求异步时,它有其自身的缺点。切换日志级别将影响所有正在进行的请求。
对于发布版本,请将以下内容添加到
proguard-rules.pro
For Apache Http Components version 5.x, you need to use
slf4j
logging Facade e.g.JUL
(Jakarta Common Logging) to bind a logging backend at compile time.Retrieve the
root
logger viaor
Set the logging level to proper level, otherwise messages are discarded
Implement your own
java.util.logging.Handler
for receiving messagesSet the handler
And for goodies a
setLevel
function for modules. A container is required to keep strong reference to uninitialized external loggers, otherwise garbage collector will deallocate andlevel
parameter will be set to default.Class to hold
modules
With function
And use it like so (I'm encapsulating everything inside
Logging
class)It has its own drawbacks when requests are async. Switching log level will affect all the in progress request.
For release build, add following to
proguard-rules.pro