How to log Apache CXF Soap request and response using Log4j

This article shows how to log inbound and outbound message using CXF interceptors.

1. Configure In and Out Logging interceptors

CXF provides interceptors for Inbound and Outbound message logging. Below the configuration to activate them.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:jaxws="http://cxf.apache.org/jaxws" 
	xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd 
http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd 
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd 
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
">

	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	
	...

	<!-- Loggers for SOAP calls -->
	<bean id="abstractLoggingInterceptor" class="org.apache.cxf.interceptor.AbstractLoggingInterceptor" abstract="true" />
	<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" parent="abstractLoggingInterceptor" />
	<bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" parent="abstractLoggingInterceptor" />

	<cxf:bus>
		<cxf:inInterceptors>
			<ref bean="loggingInInterceptor" />
		</cxf:inInterceptors>
		<cxf:outInterceptors>
			<ref bean="loggingOutInterceptor" />
		</cxf:outInterceptors>
		<cxf:outFaultInterceptors>
			<ref bean="loggingOutInterceptor" />
		</cxf:outFaultInterceptors>
		<cxf:inFaultInterceptors>
			<ref bean="loggingInInterceptor" />
		</cxf:inFaultInterceptors>
	</cxf:bus>
</beans>

Using Log4j Instead of java.util.logging

CXF by default uses java.util.logging. If you want you can log using Log4J. Under src/main/resources/META-INF/cxf/ create a file named org.apache.cxf.Logger with the following contents:

org.apache.cxf.common.logging.Log4jLogger

You may need to add a category org.apache.cxf to your Log4J configuration like below:

<category name="org.apache.cxf" additivity="false">
    <priority value="INFO" />
    <appender-ref ref="LOG" />
</category>

If you want to learn more retro games news, read our magazine and subscribe to our newsletter.