Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Tracing

Debugging techniques, as you saw in the previous sections, are tuned for development activities. Applications deployed at customer sites do not contain any debugging information. In a scenario where an application deployed at a customer site fails, you must re-create the problem in your development environment before you can identify the causes and fix the problem. In order to re-create a problem, you need to replicate the exact steps that a user took, including the data entered. That is easier said than done! Users perform numerous activities using software applications as a part of their business processes. Needless to say, there are a lot of variations that can happen in even the simplest business processes. What you need is a mechanism where the application reveals the steps that the user executed, even what data was entered. This is possible by introducing trace statements in the source code of your application.

A trace statement is a statement inserted at different points in the source code that identifies what module or component is being executed when the application is running. You can view the output of the trace statements either on the console or in a trace file.

Trace statements, like debug statements, can be designed to be switched on or off in applications. The following code snippet shows an example of a simple trace statement added to the source code:

import java.text.*;
import java.util.*;

class MyTraceHelper
{
  private static SimpleDateFormat sdf = 
      sdf.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);

  public static void trace(Object inpObj, String message)
  {
    System.out.println("*******************************");
    System.out.println("TRACE for " + inpObj);
    System.out.println("Date and Timestamp: " + sdf.format(new Date()));
    System.out.println("Message: " + message);
    System.out.println("*******************************");
  }
}

class MyApp
{
  public boolean traceFlag = false;
  MyApp()
  {
     // set the trace flag
     Properties inputProp = new Properties();
     traceFlag = Boolean.getBoolean(inputProp.getProperty("TRACE_FLAG"));
  }
  
  public void myMethod1()
  {
     ...
     // perform processing
     if(traceFlag)
     {
       MyTraceHelper.trace(inpData, "Input data has been read.");
     }
  }
}

The application MyApp uses the trace() method of the MyTraceHelper class to invoke trace statements embedded in the source code. The trace() method displays the contents of the object and the message passed as parameters to the method along with a date and time stamp. This enables easy interpretation of the trace output generated on the console.

To recap, tracing is an extension of debugging and uses techniques that are similar to debugging. The primary difference between tracing and debugging techniques is where they are applied. Unlike debugging, tracing is used when applications are deployed at customer sites rather than in a development environment.

  • + Share This
  • 🔖 Save To Your Account