动态配置Apache Http客户端 [英] Dynamically configuring Apache Http client

查看:117
本文介绍了动态配置Apache Http客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用 http://hc.apache的模块.org/httpcomponents-client-4.2.x/index.html 向外部服务发出HTTP请求.该模块将由应用程序使用.应用程序通过基于XML的配置文件配置模块的不同方面.我想在该XML文件中指定用于HTTP通信的日志记录级别.该模块将读取该配置文件,并使用该日志记录级别配置apache HTTP客户端.我找不到任何方法可以通过编程方式配置具有应用所需的正确日志记录级别的apache http库.有什么办法吗?

I am creating a module that uses http://hc.apache.org/httpcomponents-client-4.2.x/index.html to make HTTP requests to external services. This module is going to be used by application. The application configures different aspect of the module by XML based config file. and I want to specify the logging level to be used for http communication in that XML file. The module will read that config file, and configure apache HTTP client with that logging level. I could not find any way how programmatically i can configure apache http library with the right logging level the app wants. Is there any way?

推荐答案

httpclient使用公用日志记录,如下所述:

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.

这篇关于动态配置Apache Http客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆