Click here to Skip to main content
Licence 
First Posted 16 Jan 2002
Views 61,238
Bookmarked 9 times

Tracing in Java applications

By | 16 Jan 2002 | Article
A simple thread-safe Java class to dynamically generate trace info.

Introduction

I have written articles on dynamically generating trace files in multi-threaded C++ and VB (script) applications. This is an attempt to do the same for Java applications. Since every function has to belong to a class in Java, my code consists of a single class XYTrace, with three thread-safe static methods.

The first method is SetTraceFilePrefix which determines where the trace file will be created and how it will be named. Without calling this method, the default trace file will be created in the current working directory with name Trace_yyyymmdd_HHMMSSMLS.txt, where yyyymmdd_HHMMSSMLS is a date-time stamp down to milliseconds. If you make the following call anywhere in your Java program,

XYTrace.SetTraceFilePrefix("c:\\winnt\\temp\\MyTrace");

the trace file will be created in the directory c:\winnt\temp and the file name will be MyTrace_yyyymmdd_HHMMSSMLS.txt. Note that, calling this method will close the previous trace file (if there is any) and create a new one at a later time.

The second method is SetTraceLevel. It can be called at run-time to adjust the current trace level. The current trace level is just a non-negative integer, where 0 means no tracing, and the larger the current trace level is, the more info will be written to the trace file.

The third method WriteTrace is where the main work is done. This method takes two arguments, an integer representing the intended trace level and a string representing the text you want to output to the trace file. For example, you can have the following statements in your Java program,

XYTrace.WriteTrace(10, "Hello, world");
XYTrace.WriteTrace(20, "Today is "+new Date().toString());
XYTrace.WriteTrace(30, "I am feeling wild");

If the current trace level is 0, then nothing will be written to the trace file when the code is executed. If the current trace level is 20, then only the first two messages will be written. If the current trace level is 30 or above, then all three messages will be written. Note that each trace message written to the trace file will be prefixed with a string of the form HH:MM:SS_MLS(current_thread_name):, where HH:MM:SS_MLS is a time stamp.

The trace file won't get out of hand even if your program keeps running for days or weeks, because in each new day, the old trace file will be closed and a new one will be used automatically. Here is a simple test program for the XYTrace class.

/******** file: TraceTest.java *********/ 
import java.lang.Thread; 
public class TraceTest implements Runnable 
{ 
    // this function is executted in a new thread 
    // it writes 3 trace messages every 5 seconds 
    // with different trace levels 
    final public void run() 
    { 
        int nCount = 1; 
        while(true) 
        { 
            XYTrace.WriteTrace(10, "Hello, world"); 
            nCount++; 
            XYTrace.WriteTrace(20, "How are you?"); 
            nCount++; 
            XYTrace.WriteTrace(30, "This is message "+nCount); 
            nCount++; 
            try 
            { 
                Thread.currentThread().sleep(5*1000); 
            } 
            catch(Exception e) 
            { 
            } 
        } 
    } 
  
    // this is the main function 
    public static void main (String[] args) 
    { 
        // set the trace file prefix and trace level 
        XYTrace.SetTraceFilePrefix("c:\\temp\\MyTrace"); 
        XYTrace.SetTraceLevel(30); 
        // write a trace message 
        XYTrace.WriteTrace(30, "This is the main thread."); 
        // create 3 new threads 
        for(int i=0;i<3;i++) new Thread(new TraceTest()).start(); 
        // change the current trace leve every 20 seonds 
        while(true) 
        { 
            try 
            { 
                Thread.currentThread().sleep(20*1000); 
                XYTrace.SetTraceLevel(10); 
                Thread.currentThread().sleep(20*1000); 
                XYTrace.SetTraceLevel(20); 
                Thread.currentThread().sleep(20*1000); 
                XYTrace.SetTraceLevel(30); 
            } 
            catch(Exception e) 
            { 
            } 
        } 
    } 
} 
/******* end of file ************/ 

Thank you for reading my articles. Have a great day!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Xiangyang Liu 刘向阳



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmembervgrao3620:47 17 Jun '10  
GeneralSubset of java.util.logging.Logger PinmemberMember 20232154:30 7 Apr '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 17 Jan 2002
Article Copyright 2002 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid