How To Log SOAP Request and Response with Log4j in Axis 1.x

Sometimes we want to log the request that we send to server and the response from server as well. Log4j is one of the library that can help us for this kind of requirement.

First, create class that extend BasicHandler class and implements the invoke method just like this.


package com.ariestania.client.logger;

import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.commons.logging.LogFactory;

/**
 * @author ariestania.winda
 */
public class SoapLogHandler extends BasicHandler {

    @Override
    public void invoke(MessageContext mc) throws AxisFault {
        if (mc.getResponseMessage() != null && mc.getResponseMessage().getSOAPPartAsString() != null) {
            String resMsg = mc.getResponseMessage().getSOAPPartAsString();
            //print response on console
            System.out.println("Response: \r\n" + resMsg);
            //print response on log file
            LogFactory.getLog("bohayMessage").info("Response: " + resMsg);
        } else if (mc.getRequestMessage() != null && mc.getRequestMessage().getSOAPPartAsString() != null) {
            String reqMsg = mc.getRequestMessage().getSOAPPartAsString();
            //print request on console
            System.out.println("Request: \r\n" + reqMsg);
            //print request on log file
            LogFactory.getLog("bohayMessage").info("Request: " + reqMsg);
        }
    }
}


Create file config wsdd outside of your source code folder. If you want to log the request and response on the server side, give the file name to server-config.wsdd. Axis will automatically search that file. But since I create this sample on the client side, I need to name it to client-config.wsdd.
Point out the handler tag to handler class that we created before.

<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig" xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

   <handler name="log" type="java:com.ariestania.client.logger.SoapLogHandler">
      <parameter name="LogHandler.fileName" value="/tmp/axis.log" />
   </handler>

   <globalConfiguration>
      <parameter name="disablePrettyXML" value="false" />
      <requestFlow>
         <handler type="log" />
      </requestFlow>
      <responseFlow>
         <handler type="log" />
      </responseFlow>
   </globalConfiguration>

   <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender" />
   <transport name="local" pivot="java:org.apache.axis.transport.local.LocalSender" />
   <transport name="java" pivot="java:org.apache.axis.transport.java.JavaSender" />
</deployment>

I created my xml Log4j configuration like this so that I can save the log on specific format and position.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
    
    <!-- INFO -->
    <appender name="bohayMessage" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="datePattern" value="'.'yyyy-MM-dd" />
        <param name="File" value="logs/bohaymessage.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{MM-dd HH:mm:ss.SSS} - %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="info" />
            <param name="LevelMax" value="info" />
        </filter>
    </appender>
        
    <category name="bohayMessage" additivity="false">
        <appender-ref ref="bohayMessage" />
    </category>
        
    <root>
        <appender-ref ref="bohayMessage" />
    </root>    
        
</log4j:configuration>

The result on my log looks like this.

I have put my project on my Github. Hope it can be help. Thanks to Raspayu on stackoverflow for the clear explanation.
Happy coding 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s