Configure Log4j with CXF

Configure Log4j with CXF explains step by step details of configuring Log4j and Apache CXF Webservice.

For every application logging has a vital role because it is a must for various debugging purpose, CXF framework have in-built feature for enabling logging of request and response payloads

log4j is commonly used logging framework in java, this tutorial is using log4j as the logging tool.

You can also see server side/client side logging Apache CXF Logging

Here I am showing How to use Log4j for logging CXF soap messages

I am going to reuse my CXF Web Service Tutorial

You need to download following additional library

  1. log4j2

Following jars must be in classpath

  1. cxf-core-3.3.6.jar
  2. cxf-rt-bindings-soap-3.3.6.jar
  3. cxf-rt-databinding-jaxb-3.3.6.jar
  4. cxf-rt-features-logging-3.3.6.jar
  5. cxf-rt-frontend-jaxws-3.3.6.jar
  6. cxf-rt-frontend-simple-3.3.6.jar
  7. cxf-rt-transports-http-3.3.6.jar
  8. cxf-rt-wsdl-3.3.6.jar
  9. httpasyncclient-4.1.4.jar
  10. httpclient-4.5.12.jar
  11. httpcore-4.4.13.jar
  12. httpcore-nio-4.4.13.jar
  13. log4j-api-2.13.3.jar
  14. log4j-core-2.13.3.jar
  15. log4j-slf4j-impl-2.13.2.jar
  16. neethi-3.1.1.jar
  17. slf4j-api-1.7.29.jar
  18. spring-aop-5.1.14.RELEASE.jar
  19. spring-beans-5.1.14.RELEASE.jar
  20. spring-context-5.1.14.RELEASE.jar
  21. spring-core-5.1.14.RELEASE.jar
  22. spring-expression-5.1.14.RELEASE.jar
  23. spring-jcl-5.1.14.RELEASE.jar
  24. spring-web-5.1.14.RELEASE.jar
  25. stax2-api-3.1.4.jar
  26. woodstox-core-5.0.3.jar
  27. wsdl4j-1.6.3.jar
  28. xmlschema-core-2.2.5.jar

Please see the project structure on below screenshot

Log4j with CXF

Now you need to create two additional files, 1) org.apache.cxf.Logger 2) log4j.properties, you can see the file name and its contents below, please drop both of these files according to the above screen shot.

org.apache.cxf.Logger

org.apache.cxf.common.logging.Slf4jLogger

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">

	<!-- PROPERTIES -->
	<Properties>
		<Property name="layout">%d{HH:mm:ss.SSS}
			%-5level[%thread][%logger{0}] %m%n</Property>
		<Property name="cxfLogFile">cxf-soap-logs</Property>
	</Properties>

	<!-- APPENDERS -->
	<Appenders>
		<RollingFile name="CXF_LOG_FILE" fileName="${cxfLogFile}.log"
			filePattern="${cxfLogFile}.%d{yyyy-MM-dd}.log">
			<TimeBasedTriggeringPolicy interval="1" modulate="true" />
			<DefaultRolloverStrategy max="30" />
			<PatternLayout pattern="${layout}" />
		</RollingFile>
	</Appenders>

	<!-- LOGGERS -->
	<Loggers>
		<Logger name="org.apache.cxf.services" level="INFO" additivity="false">
			<!-- specify a dedicated appender for the SOAP messages -->
			<AppenderRef ref="CXF_LOG_FILE" />
		</Logger>
	</Loggers>

</Configuration>

cxf.xml

Modify cxf.xml (add inInterceptors & outInterceptors) in order to show SOAP inbound/outbound messages

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:cxf="http://cxf.apache.org/core"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd  
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	
<cxf:bus>
    <cxf:features>
        <bean class="org.apache.cxf.ext.logging.LoggingFeature">
            <property name="prettyLogging" value="true"/>
        </bean>
    </cxf:features>
</cxf:bus>

<jaxws:endpoint id="changeStudent" implementor="com.student.ChangeStudentDetailsImpl"
		address="/ChangeStudent" />
</beans>

you can also see CXF client example in order to run this service

After running the client, you can see the logs on console as well as configured directory, in our case it is cxf-soap-logs.txt

cxf-soap-logs.txt

11:56:57.427
INFO [http-nio-8080-exec-6][REQ_IN] REQ_IN
    Address: http://localhost:8080/CXFTutorial/ChangeStudent?wsdl
    HttpMethod: POST
    Content-Type: text/xml; charset=UTF-8
    ExchangeId: c964914f-1d12-4806-bfb8-c84cfa8cc33e
    ServiceName: ChangeStudentDetailsImplService
    PortName: ChangeStudentDetailsImplPort
    PortTypeName: ChangeStudentDetails
    Headers: {SOAPAction="", Accept=*/*, host=localhost:8080, connection=keep-alive, content-type=text/xml; charset=UTF-8, cache-control=no-cache, Content-Length=206, pragma=no-cache, user-agent=Apache-CXF/3.3.6}
    Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ns2:changeName xmlns:ns2="http://student.com/">
      <arg0>
        <name>Rockey</name>
      </arg0>
    </ns2:changeName>
  </soap:Body>
</soap:Envelope>


11:56:57.450
INFO [http-nio-8080-exec-6][RESP_OUT] RESP_OUT
    Address: http://localhost:8080/CXFTutorial/ChangeStudent?wsdl
    Content-Type: text/xml
    ResponseCode: 200
    ExchangeId: c964914f-1d12-4806-bfb8-c84cfa8cc33e
    ServiceName: ChangeStudentDetailsImplService
    PortName: ChangeStudentDetailsImplPort
    PortTypeName: ChangeStudentDetails
    Headers: {}
    Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ns2:changeNameResponse xmlns:ns2="http://student.com/">
      <return>
        <name>Hello Rockey</name>
      </return>
    </ns2:changeNameResponse>
  </soap:Body>
</soap:Envelope>








3 Responses to "Configure Log4j with CXF"
  1. alkis 2012-07-12 09:55:48.0
  1. admin 2012-07-13 09:55:48.0
  1. Milind Pawar 2020-06-04 09:11:58.0

Your email address will not be published. Required fields are marked *