Logging PracticesBeing a library HttpClient is not to dictate which logging framework the user has to use. Therefore HttpClient utilizes the logging interface provided by the Jakarta Commons Logging package. Commons Logging provides a simple and generalized log interface to various logging packages. By using Commons Logging, HttpClient can be configured for a variety of different logging behaviours. That means the user will have to make a choice which logging framework to use. By default Commons Logging supports the following logging frameworks:
HttpClient performs two different kinds of logging: the standard context logging used within each class, and wire logging. Context Logging
Context logging contains information about the internal operation
of HttpClient as it performs HTTP requests. Each class has its own
log named according to the class's fully qualified name. For example
the class Wire LoggingThe wire log is used to log all data transmitted to and from servers when executing HTTP requests. This log should only be enabled to debug problems, as it will produce an extremely large amount of log data, some of it in binary format.
Because the content of HTTP requests is usually less important for debugging
than the HTTP headers, these two types of data have been separated into
different wire logs. The content log is Configuration ExamplesCommons Logging can delegate to a variety of loggers for processing the actual output. Below are configuration examples for Commons Logging, Log4j and java.util.logging. Commons Logging Examples
Commons Logging comes with a basic logger called
Note: The system properties must be set before a reference to any Commons Logging class is made.
Enable header wire + context logging - Best for Debugging
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
Enable full wire(header and content) + context logging
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
Enable just context logging
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); Log4j ExamplesThe simplest way to configure Log4j is via a log4j.properties file. Log4j will automatically read and configure itself using a file named log4j.properties when it's present at the root of the application classpath. Below are some Log4j configuration examples. Note: Log4j is not included in the HttpClient distribution.
Enable header wire + context logging - Best for Debugging
log4j.rootLogger=INFO, stdout
Enable full wire(header and content) + context logging
log4j.rootLogger=INFO, stdout
Log wire to file + context logging
log4j.rootLogger=INFO
Enable just context logging
log4j.rootLogger=INFO, stdout Note that the default configuration for Log4J is very inefficient as it causes all the logging information to be generated but not actually sent anywhere. The Log4J manual is the best reference for how to configure Log4J. It is available at http://logging.apache.org/log4j/docs/manual.html java.util.logging Examples
Since JDK 1.4 there has been a package
java.util.logging that provides a
logging framework similar to Log4J. By default it reads a config file from
handlers=java.util.logging.ConsoleHandlerTo customize logging a custom logging.properties file should be created
in the project directory. The location of this file must be passed to the JVM as a
system property. This can be done on the command line like so:
$JAVA_HOME/java -Djava.util.logging.config.file=$HOME/myapp/logging.properties -classpath $HOME/myapp/target/classes com.myapp.MainAlternatively LogManager#readConfiguration(InputStream) can be used to pass it the desired configuration.
Enable header wire + context logging - Best for Debugging
.level=INFO
Enable full wire(header and content) + context logging
.level=INFO
Enable just context logging
.level=INFO More detailed information is available from the Java Logging documentation. |