Spring WS Logging Soap Messages
When developing or debugging a Soap Web Service, you want to inspect the request and/or response, to see what you are sending/receiving. Spring WS offers this functionallity via the standard Commons Logging interface. In the previous tutorial we saw how to produce and consume a Spring WS Soap Service. In this example we show you how to log the outgoing request and incomming response using logback or log4j.
Project Structure
You must add your logging file on the classpath. In this example we put the logback.xml file in the resources folder. If you want to work with log4j you need to add a log4j.properties file here.
src
|--main
| +--java
| +--com
| +--memorynotfound
| +--server
| |--...
| +--resources
| |--logback.xml
| +--...
pom.xml
Spring WS Logging soap request response
Logging server-side messages
For logging all the server-side messages (request and response) you must set the org.springframework.ws.server.MessageTracing
logger to level DEBUG or TRACE. When DEBUG is enabled, only the payload root element is logged. When the TRACE is enabled, the entire message content is logged. If you want to log sent messages, use the org.springframework.ws.server.MessageTracing.sent
logger. The org.springframework.ws.server.MessageTracing.received
logger is used for logging only the received messages.
Logging client-side messages
For logging messages on the client side, simular loggers exist: org.springframework.ws.client.MessageTracing.sent
and org.springframework.ws.client.MessageTracing.received
.
Logging with Logback
Here is an example logback.xml
configuration.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springframework.ws.client.MessageTracing.sent" level="TRACE"/>
<logger name="org.springframework.ws.client.MessageTracing.received" level="TRACE"/>
<logger name="org.springframework.ws.server.MessageTracing" level="TRACE"/>
</configuration>
Logging with Log4j
Here is an example log4j.properties
configuration.
log4j.rootCategory=INFO, stdout
log4j.logger.org.springframework.ws.client.MessageTracing.sent=TRACE
log4j.logger.org.springframework.ws.client.MessageTracing.received=TRACE
log4j.logger.org.springframework.ws.server.MessageTracing=TRACE
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p [%c{3}] %m%n
Example Logging Output
Typical logging output would be
2016-01-05 11:36:43.482 TRACE 80573 --- [nio-8080-exec-1] o.s.ws.server.MessageTracing.received : Received request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:getBeerRequest xmlns:ns2="https://memorynotfound.com/beer"><ns2:id>1</ns2:id></ns2:getBeerRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>]
2016-01-05 11:36:43.605 TRACE 80573 --- [nio-8080-exec-1] o.s.ws.server.MessageTracing.sent : Sent response [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:getBeerResponse xmlns:ns2="https://memorynotfound.com/beer"/></SOAP-ENV:Body></SOAP-ENV:Envelope>] for request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:getBeerRequest xmlns:ns2="https://memorynotfound.com/beer"><ns2:id>1</ns2:id></ns2:getBeerRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>]
Thanks :)
log4j logging works great
Any idea how to get it to log the HTTP headers as well?
Hi Kevin,
For as far I know, Spring WS does not have any default interceptor configured to log the HTTP Headers, only the soap request and/or response body.
You can however, write your own interceptor based on the PayloadLoggingInterceptor from Spring WS. Using an interceptor on the client or on the server, you can inspect the entire message. And are able to write the message to a log file.
You can find how to write a custom interceptor here: http://memorynotfound.com/spring-ws-intercept-request-response-soap-messages/