且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动态配置Apache Http客户端

更新时间:2021-10-07 21:59:40

httpclient 使用公共日志记录,如下所述:http://hc.apache.org/httpcomponents-client-4.2.x/logging.html

httpclient uses commons logging, as explained here: http://hc.apache.org/httpcomponents-client-4.2.x/logging.html

因此它将日志记录委托给您的日志记录框架.要配置 http 请求的日志记录,您需要使用日志记录框架的 API.例如,如果使用 JDK 日志记录,这样的事情应该可以工作:

So it delegates logging to your logging framework. To configure logging of http requests, you need to use the API of the logging framework. For instance, if using JDK logging, something like this should work:

java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(Level.ALL)

每个日志框架都有自己的 API.

Each logging framework will have its own API.

要使用与 commons-logging 一起打包的内置 SimpleLog 实现,您可以执行以下操作:

To use the built-in SimpleLog implementation that is packaged with commons-logging, you could do something like this:

    System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog"); 
    System.setProperty("org.apache.commons.logging.simplelog.defaultlog","trace"); 

    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://www.google.com");
    client.execute(request);

运行此代码应该会向控制台 (syserr) 打印大量日志输出.

Running this code should print a lot of log outputs to the console (syserr).

请注意,对于生产日志记录框架来说,simplelog 并不是一个真正的好选择.你真的应该使用类似 log4j 的东西.

Note that simplelog is not really a good choice for a production logging framework. You should really use something like log4j.