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), you can see the file name and its contents below, please drop both of these files according to the above screen shot.




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

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

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

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



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

<beans xmlns="" xmlns:cxf=""
	xmlns:xsi="" xmlns:jaxws=""
        <bean class="org.apache.cxf.ext.logging.LoggingFeature">
            <property name="prettyLogging" value="true"/>

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

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


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="">
    <ns2:changeName xmlns:ns2="">

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="">
    <ns2:changeNameResponse xmlns:ns2="">
        <name>Hello Rockey</name>

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 *