IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope: Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  Java technology  >

Techniques for adding trace statements to your Java application

Use tracing during development as well as after your application is deployed

developerWorks


File PrintMessage.java:
				
/*                                               
 * Class Name:   PrintMessage
 *
 * Purpose:  Provide development time trace capabilities
 *
 * Owner:
 * Version: 1.0
 */
import java.text.DateFormat;   
import java.util.Date;                 
public class PrintMessage
{
  static DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
                                                        DateFormat.MEDIUM);
  /* Development time trace variables */
  public static final boolean traceLow = ((SharedConstants.DEBUG 
                                         SharedConstants.TRACE_LOW) == 
                                         SharedConstants.TRACE_LOW)
                                         ? true : false;
  public static final boolean traceMedium = ((SharedConstants.DEBUG 
                                            SharedConstants.TRACE_MEDIUM) ==
                                            SharedConstants.TRACE_MEDIUM)
                                            ? true : false;
  public static final boolean traceHigh = ((SharedConstants.DEBUG 
                                          SharedConstants.TRACE_HIGH) == 
                                          SharedConstants.TRACE_HIGH) 
                                          ? true : false;
  public static final boolean traceAll = ((SharedConstants.DEBUG 
                                         SharedConstants.TRACE_ALL) == 
                                         SharedConstants.TRACE_ALL) 
                                         ? true : false;
   /* The following set of functions are made available for your application */
   /* code.  Write a re-usable set of methods allows implementation of trace */
   /* output to be controlled from one central place.  Later, the output     */
   /* location can be changed, the format of the output message can be       */
   /* changed, etc.                                                          */
   /* Output trace functions */
   public static void printMessageWithDateTime(String msg)
   {
      msg = new String(df.format(new Date()) + ":  " + msg);
      print(msg, true);
      return;
   }
   public static void printMessage(String msg)
   {
      print(msg, true);
      return;
   }
   public static void printMessageNoNewLine(String msg)
   {
      print(msg, false);
      return;
   }
   /* private method that all public print messages call to output msg */
   private static void print(String msg, boolean newLine)
   {
      if (newLine)
         System.out.println(msg);
      else
         System.out.print(msg);
      return;
   }
   private static final String COPYRIGHT = SharedConstants.COPYRIGHT;
}
      

Return to the article.

    关于 IBM 隐私条约 联系 IBM 使用条款