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 {

    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="" xmlns:java="">

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

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

   <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="" />

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" >
    <!-- 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" />
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="info" />
            <param name="LevelMax" value="info" />
    <category name="bohayMessage" additivity="false">
        <appender-ref ref="bohayMessage" />
        <appender-ref ref="bohayMessage" />

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 🙂

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s