Adding logging to MathService

Enabling logging in our Grid Service is very easy. The following code shows all the necessary changes in bold. We'll take a closer look in a moment.

// ...

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

// ...

public class MathImpl extends GridServiceImpl implements MathPortType
{
  
  // Create this class's logger
  static Log logger = LogFactory.getLog(MathImpl.class.getName());

  // ...

  public void add(int a) throws RemoteException
  {
    
    logger.info("Addition invoked with parameter a=" + String.valueOf(a));
    if (a==0)
      logger.warn("Adding zero doesn't modify the internal value!");
    value = value + a;
  }

  public void subtract(int a) throws RemoteException
  {
    
    logger.info("Subtraction invoked with parameter a=" + String.valueOf(a));
    if (a==0)
      logger.warn("Subtracting zero doesn't modify the internal value!");
    value = value - a;
  }

  public int getValue() throws RemoteException
  {
    logger.info("getValue() invoked");
    return value;
  }
}
[Note]

Add these modifications to $TUTORIAL_DIR/org/globus/progtutorial/services/core/first/impl/MathImpl.java and save it as $TUTORIAL_DIR/org/globus/progtutorial/services/core/logging/impl/MathImpl.java

As you can see, the modifications are very simple. First of all, we need to import two packages from the Commons Logging component. After that, we need to create a static Log attribute. This attribute is created using a LogFactory. Notice how we have to pass the name of our class to the getLog method.

static Log logger = LogFactory.getLog(MathImpl.class.getName());

After these two modifications, our MathImpl class is ready to do some serious logging. We're going to generate some Info and Warn messages. This is as simple as calling the info or warn method in the logger static attribute. The only necessary parameter is the message we want to write in the log.

logger.info("Addition invoked with parameter a=" + String.valueOf(a));
if (a==0)
  logger.warn("Adding zero doesn't modify the internal value!");

As you can see, we're generating an info message every time any of the methods is invoked, and a warning message whenever the add or subtract method is called with an argument equal to zero.

Of course, you can generate messages in any of the other levels by calling:

Writing the deployment descriptor

The deployment descriptor will be very similar to the first example's descriptor. The only important change is the GSH of the service and the baseClassName of the service (which is the class we've just programmed: the first example's class plus logging code)

<?xml version="1.0"?>
<deployment name="defaultServerConfig" xmlns="http://xml.apache.org/axis/wsdd/"
  xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

  <service name="progtutorial/core/logging/MathService" provider="Handler" style="wrapped">
    <parameter name="name" value="MathService"/>
    <parameter name="className" value="org.globus.progtutorial.stubs.MathService.MathPortType"/>
    <parameter name="baseClassName" value="org.globus.progtutorial.services.core.logging.impl.MathImpl"/>
    <parameter name="schemaPath" value="schema/progtutorial/MathService/Math_service.wsdl"/>

    <!-- Start common parameters -->
    <parameter name="allowedMethods" value="*"/>
    <parameter name="persistent" value="true"/>
    <parameter name="handlerClass" value="org.globus.ogsa.handlers.RPCURIProvider"/>
  </service>

</deployment>
[Note]

This file is $TUTORIAL_DIR/org/globus/progtutorial/services/core/logging/server-deploy.wsdd

Generate GAR and deploy

Let's compile the service:

./tutorial_build.sh \
org/globus/progtutorial/services/core/logging \
schema/progtutorial/MathService/Math.gwsdl

And deploy it:

ant deploy \
-Dgar.name=$TUTORIAL_DIR/build/lib/org_globus_progtutorial_services_core_logging.gar