Logging request/response with Apache CXF as XML Logging request/response with Apache CXF as XML xml xml

Logging request/response with Apache CXF as XML


So, I tried a little more with this. To get the XML Request and Replies logged, and if you are using Log4J, you need to set the Log-level of CXF in the log4j.xml file like this (>= INFO):

<logger name="org.apache.cxf" >    <level value="INFO" /></logger>

And the cxf.xml file should contains this:

<cxf:bus>    <cxf:features>        <cxf:logging/>    </cxf:features></cxf:bus> 

Both files should be in the CLASSPATH.

To display the soap message add this to your code:

Client client = ClientProxy.getClient(service);client.getInInterceptors().add(new LoggingInInterceptor());client.getOutInterceptors().add(new LoggingOutInterceptor());


The request soap xml can be logged easily by an custom In interceptor. Say, we have an interceptor named "wsLoggingInInterceptor", So in the context file it will be like the following :

<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/><bean id="logOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/><bean id="wsLoggingInInterceptor" class="org.jinouts.webservice.logging.WSLoggingInInterceptor"/>   <cxf:bus>        <cxf:inInterceptors>            <ref bean="loggingInInterceptor"/>            <ref bean="wsLoggingInInterceptor"/>        </cxf:inInterceptors>        <cxf:outInterceptors>            <ref bean="logOutInterceptor"/>                   </cxf:outInterceptors>    </cxf:bus>

In the class we can get the request xml as follows:

public class WSLoggingInInterceptor extends AbstractSoapInterceptor{    public WSLoggingInInterceptor ()    {        super(Phase.RECEIVE);    }        @Override    public void handleMessage ( SoapMessage message ) throws Fault    {        //get the remote address        HttpServletRequest httpRequest = (HttpServletRequest) message.get ( AbstractHTTPDestination.HTTP_REQUEST );        System.out.println ("Request From the address : " + httpRequest.getRemoteAddr ( ) );                try        {            // now get the request xml            InputStream is = message.getContent ( InputStream.class );            CachedOutputStream os = new CachedOutputStream ( );            IOUtils.copy ( is, os );            os.flush ( );            message.setContent (  InputStream.class, os.getInputStream ( ) );            is.close ( );                        System.out.println ("The request is: " + IOUtils.toString ( os.getInputStream ( ) ));            os.close ( );        }                catch ( Exception ex )        {            ex.printStackTrace ( );        }            }}

Look, here I have also log the address from where the request is coming. You can also get some more information from the "HttpServletRequest" object. you can have more from :http://cxf.apache.org/docs/interceptors.html

To log response xml you can have a look at this thread


Add the following to your endpoints and clients:

<jaxws:features>    <bean class="org.apache.cxf.feature.LoggingFeature" /></jaxws:features>

This will log everything to the server log.

If you want to log them elsewhere, then look at the source code of the built-in CXF LoggingInInterceptor and LoggingOutInterceptor. You can follow the pattern they use to grab the messages on their way in/out and do with them what you like.

Add your own interceptors to the chain with something like this:

<jaxws:inInterceptors>    <ref bean="myLoggingInInterceptor" /></jaxws:inInterceptors>