0

This may sounds like a silly question, but I can't find the answer and it's really bothering me. I'm using logback for logging and I'm currently working with some soap requests using spring ws. What I want is to log whole soap request as a nicely formatted xml in console (multiple rows). I've added logger for "org.springframework.ws" with "trace" level and what I got is exactly one line per each request.

My logback.xml:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="error">
        <appender-ref ref="STDOUT" />
    </root>

    <logger name="org.springframework.ws" level="trace" />
</configuration>

How to enable this feature?

jantobola
  • 688
  • 2
  • 10
  • 28

2 Answers2

0

Most SOAP libraries, including Spring-WS, do not format the message before sending it. The newlines, tabs and spaces are not relevant for the receiving party and it saves some bandwidth.

Having said that, you might want to add a custom ClientInterceptor that intercepts each message. You should implement handleRequest(MessageContext) and/or handleResponse(MessageContext) (depending on whether you are sending or receiving the messages).

To get the raw XML, write code like so:

final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
    message.writeTo(baos);
} catch (IOException ioe) {
    log.error("Could not intercept payload", ioe);
    throw new WebServiceIOException("Could not intercept payload", ioe);
}
final String payload = baos.toString("UTF-8");

There are lots of questions and answers around at SO about formatting XML messages, so that is left as an exercise to the reader.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
0

The simplest yet most convinient and powerfull solution is to create your own Log4j appender to pretty print your XML message. It offers you strong possibilities of configuration without bloating your network stack:

How to create your own appender using Log4j2

How to pretty print XML

also take a look how you can get a bit more granularity with spring loggers.

Community
  • 1
  • 1
B.Gen.Jack.O.Neill
  • 8,169
  • 12
  • 51
  • 79