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
Following jars must be in classpath
- cxf-core-3.3.6.jar
- cxf-rt-bindings-soap-3.3.6.jar
- cxf-rt-databinding-jaxb-3.3.6.jar
- cxf-rt-features-logging-3.3.6.jar
- cxf-rt-frontend-jaxws-3.3.6.jar
- cxf-rt-frontend-simple-3.3.6.jar
- cxf-rt-transports-http-3.3.6.jar
- cxf-rt-wsdl-3.3.6.jar
- httpasyncclient-4.1.4.jar
- httpclient-4.5.12.jar
- httpcore-4.4.13.jar
- httpcore-nio-4.4.13.jar
- log4j-api-2.13.3.jar
- log4j-core-2.13.3.jar
- log4j-slf4j-impl-2.13.2.jar
- neethi-3.1.1.jar
- slf4j-api-1.7.29.jar
- spring-aop-5.1.14.RELEASE.jar
- spring-beans-5.1.14.RELEASE.jar
- spring-context-5.1.14.RELEASE.jar
- spring-core-5.1.14.RELEASE.jar
- spring-expression-5.1.14.RELEASE.jar
- spring-jcl-5.1.14.RELEASE.jar
- spring-web-5.1.14.RELEASE.jar
- stax2-api-3.1.4.jar
- woodstox-core-5.0.3.jar
- wsdl4j-1.6.3.jar
- xmlschema-core-2.2.5.jar
Please see the project structure on below screenshot

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>