Enable debug logging for Log4J2 + Apache HttpClient

im trying to activate debugging logging for my Apache HttpClient, but cant get it working (without logging to anything related to HttpClient).

This is my log4j2 configuration currently in use:

<?xml version="1.0" encoding="UTF-8"?> <configuration status="OFF"> <appenders> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" /> </Console> <RollingFile name="RollingRandomAccessFile" fileName="logs/test.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz"> <PatternLayout> <Pattern> %d %p %c{1.} [%t] %m%n </Pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="10 MB" /> </Policies> <DefaultRolloverStrategy max="20" /> </RollingFile> <Async name="async"> <AppenderRef ref="RollingRandomAccessFile" /> </Async> </appenders> <loggers> <logger name="org.apache.http.wire" level="debug" /> <logger name="org.apache.http.client" level="debug" /> <logger name="org.apache.xerces.parsers.SAXParser" level="warn" /> <logger name="org.hibernate" level="warn" /> <root level="trace"> <appender-ref ref="console" /> <appender-ref ref="async" /> </root> </loggers> </configuration> 

Changing the level from warning to debugging for sleep mode works fine, for example.

These libs im use:

 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>fluent-hc</artifactId> <version>4.2.6</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.2.6</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.2.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient-cache</artifactId> <version>4.2.6</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.2.6</version> </dependency> 

Log4j2

 <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.0-beta9</version> </dependency> 

Does anyone have any ideas? I have tried different package names already, for example

 httpclient.wire httpclient.wire.header httpclient.wire.content 

and a few more, but nothing works ...

+8
java logging apache log4j2
source share
2 answers

I assume httpcomponents uses log4j-1.2 internally. Log4j2 provides an adapter that routes calls from your application that use API 1.2 to implement the new 2.0.

To enable this, you only need to add the log4j-core and log4j-1.2-api banks in your classpath. (In your current Maven dependencies, there is only log4j-api, which is the new API 2.0.) See also http://logging.apache.org/log4j/2.x/faq.html .

+8
source

Remko is right that the problem is with http components using log4j-1.2 internally. The adapter that routes log4j 1.2 to log4j 2 is called the log4j 1.2 bridge and is described here: https://logging.apache.org/log4j/2.x/log4j-1.2-api/index.html .

The maven artifacts page for log4j explains how to configure your maven dependencies to include a bridge library:

Log4j 1.x API Bridge

If existing components use Log4j 1.x and you want to have this log routed to Log4j 2, then removes any log4j 1.x dependencies and adds the following.

 <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-1.2-api</artifactId> <version>2.5</version> </dependency> </dependencies> 

From https://logging.apache.org/log4j/2.x/maven-artifacts.html

+2
source

All Articles