Dynamic Apache Http Client Configuration

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 will be used by the application. The application configures various aspects of the module using an XML-based configuration file. and I want to specify the logging level that will be used to communicate with http in this XML file. The module will read this configuration file and configure the apache HTTP client with this level of logging. I could not find a way how, programmatically, I can configure the apache http library with the correct logging level that the application wants. Is there any way?

0
source share
1 answer

httpclient uses a community entry as described here: http://hc.apache.org/httpcomponents-client-4.2.x/logging.html

In this way, he delegates access to your logging system. To configure the logging of HTTP requests, you need to use the registration framework API. For example, if you use JDK logging, something like this should work:

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

Each logging environment will have its own API.

To use the built-in SimpleLog implementation, packaged using public record keeping, you can 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); 

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

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

0
source

All Articles