Skip to content

Commit bd6a2b2

Browse files
veitaok2c
authored andcommitted
Fix for HTTPCLIENT-2238
Provide logging configuration examples for the HTTP client 4.5.x for Log4J 2 instead for Log4J 1.
1 parent ae4eab2 commit bd6a2b2

1 file changed

Lines changed: 105 additions & 64 deletions

File tree

  • src/site/markdown/httpcomponents-client-4.5.x

src/site/markdown/httpcomponents-client-4.5.x/logging.md

Lines changed: 105 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ using `Commons Logging`, HttpClient can be configured for a variety of different
2828
will have to make a choice which logging framework to use. By default `Commons Logging` supports the following logging
2929
frameworks:
3030

31-
* [Log4J](http://logging.apache.org/log4j/docs/index.html)
31+
* [Log4J 2](https://logging.apache.org/log4j/2.x/index.html)
3232

3333
* [java.util.logging](http://docs.oracle.com/javase/7/docs/api/java/util/logging/package-summary.html)
3434

@@ -66,8 +66,7 @@ the `org.apache.http.headers` logging category for capturing HTTP headers only.
6666
### Configuration Examples
6767

6868
`Commons Logging` can delegate to a variety of loggers for processing the actual output. Below are configuration
69-
examples for `Commons Logging`, `Log4j` and
70-
`java.util.logging`.
69+
examples for `Commons Logging`, `Log4j 2` and `java.util.logging`.
7170

7271
## Commons Logging Examples
7372

@@ -111,68 +110,110 @@ system properties through JVM process arguments at the start up.
111110
-Dorg.apache.commons.logging.simplelog.log.org.apache.http.client=DEBUG
112111
```
113112

114-
## Log4j Examples
113+
## Log4j 2 Examples
114+
115+
The simplest way to [configure](https://logging.apache.org/log4j/2.x/manual/configuration.html) `Log4j 2` is via
116+
a `log4j2.xml` file. `Log4j 2`
117+
will [automatically](https://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguration) configure
118+
itself using a file named `log4j2.xml` when it's present at the root of the application classpath.
119+
120+
Below are some `Log4j` configuration examples.
121+
122+
**Note:** The `Log4j 2` implementation a.k.a "core" is not included in the `HttpClient` distribution. You can include it
123+
in your project using [Maven, Ivy, Gradle, or SBT](https://logging.apache.org/log4j/2.x/maven-artifacts.html).
124+
125+
- Enable header wire + context logging - **Best for Debugging**
126+
127+
```
128+
<Configuration>
129+
<Appenders>
130+
<Console name="Console">
131+
<PatternLayout pattern="%d %-5level [%logger] %msg%n%xThrowable" />
132+
</Console>
133+
</Appenders>
134+
<Loggers>
135+
<Logger name="org.apache.http" level="DEBUG">
136+
<AppenderRef ref="Console"/>
137+
</Logger>
138+
<Logger name="org.apache.http.wire" level="DEBUG">
139+
<AppenderRef ref="Console"/>
140+
</Logger>
141+
<Root level="INFO">
142+
<AppenderRef ref="Console" />
143+
</Root>
144+
</Loggers>
145+
</Configuration>
146+
```
147+
148+
- Enable full wire + context logging
149+
150+
```
151+
<Configuration>
152+
<Appenders>
153+
<Console name="Console">
154+
<PatternLayout pattern="%d %-5level [%logger] %msg%n%xThrowable" />
155+
</Console>
156+
</Appenders>
157+
<Loggers>
158+
<Logger name="org.apache.http" level="DEBUG">
159+
<AppenderRef ref="Console"/>
160+
</Logger>
161+
<Root level="INFO">
162+
<AppenderRef ref="Console" />
163+
</Root>
164+
</Loggers>
165+
</Configuration>
166+
```
167+
168+
- Enable context logging for connection management
169+
170+
```
171+
<Configuration>
172+
<Appenders>
173+
<Console name="Console">
174+
<PatternLayout pattern="%d %-5level [%logger] %msg%n%xThrowable" />
175+
</Console>
176+
</Appenders>
177+
<Loggers>
178+
<Logger name="org.apache.http.impl.conn" level="DEBUG">
179+
<AppenderRef ref="Console"/>
180+
</Logger>
181+
<Root level="INFO">
182+
<AppenderRef ref="Console" />
183+
</Root>
184+
</Loggers>
185+
</Configuration>
186+
```
187+
188+
- Enable context logging for connection management / request execution
189+
190+
```
191+
<Configuration>
192+
<Appenders>
193+
<Console name="Console">
194+
<PatternLayout pattern="%d %-5level [%logger] %msg%n%xThrowable" />
195+
</Console>
196+
</Appenders>
197+
<Loggers>
198+
<Logger name="org.apache.http.impl.conn" level="DEBUG">
199+
<AppenderRef ref="Console"/>
200+
</Logger>
201+
<Logger name="org.apache.http.impl.client" level="DEBUG">
202+
<AppenderRef ref="Console"/>
203+
</Logger>
204+
<Logger name="org.apache.http.client" level="DEBUG">
205+
<AppenderRef ref="Console"/>
206+
</Logger>
207+
<Root level="INFO">
208+
<AppenderRef ref="Console" />
209+
</Root>
210+
</Loggers>
211+
</Configuration>
212+
```
213+
214+
The `Log4J 2` manual is the best reference for how to configure `Log4J 2`. It is available at
215+
[https://logging.apache.org/log4j/2.x/manual/](https://logging.apache.org/log4j/2.x/manual/).
115216
116-
The simplest way to configure `Log4j` is via a `log4j.properties` file. `Log4j` will automatically read and configure
117-
itself using a file named `log4j.properties` when it's present at the root of the application classpath. Below are
118-
some `Log4j` configuration examples.
119-
120-
**Note:** `Log4j` is not included in the `HttpClient` distribution.
121-
122-
* Enable header wire + context logging - **Best for Debugging**
123-
124-
```
125-
log4j.rootLogger=INFO, stdout
126-
127-
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
128-
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
129-
log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n
130-
131-
log4j.logger.org.apache.http=DEBUG
132-
log4j.logger.org.apache.http.wire=ERROR
133-
```
134-
135-
* Enable full wire + context logging
136-
137-
```
138-
log4j.rootLogger=INFO, stdout
139-
140-
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
141-
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
142-
log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n
143-
144-
log4j.logger.org.apache.http=DEBUG
145-
```
146-
147-
* Enable context logging for connection management
148-
149-
```
150-
log4j.rootLogger=INFO, stdout
151-
152-
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
153-
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
154-
log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n
155-
156-
log4j.logger.org.apache.http.impl.conn=DEBUG
157-
```
158-
159-
* Enable context logging for connection management / request execution
160-
161-
```
162-
log4j.rootLogger=INFO, stdout
163-
164-
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
165-
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
166-
log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n
167-
168-
log4j.logger.org.apache.http.impl.conn=DEBUG
169-
log4j.logger.org.apache.http.impl.client=DEBUG
170-
log4j.logger.org.apache.http.client=DEBUG
171-
```
172-
173-
Note that the default configuration for Log4J is very inefficient as it causes all the logging information to be
174-
generated but not actually sent anywhere. The `Log4J` manual is the best reference for how to configure `Log4J`. It is
175-
available at http://logging.apache.org/log4j/docs/manual.html .
176217
177218
### java.util.logging Examples
178219

0 commit comments

Comments
 (0)