Show TOC

Sample Java Code with LoggingLocate this document in the navigation structure

Use

Proceed as follows:

  1. Identify the source area and get a handle to a source object: Location .

    Name the location as a complete package of the class, for example, com.sap.fooPackage.FooClass .

  2. Insert messages at points where you want to trace the program code.

  3. Assign a severity to the trace message

    Note

    The trace message will be logged if only its severity is higher than or equal to the default severity of the location - WARNING.

The common places are: entering/exiting a method, upon throwing an exception, after performing certain critical tasks, and so on.

Sample Code Without Tracing Inserted

package com.sap.fooPackage;
public class FooClass {
    public void method1() {
       … … … }
    public void method2() {
       try {
          … ….} catch (IOException e) {
          … …..}
    }
    public static void main(String[] args) {
       FooClass test1 = new FooClass();
       test1.method1();
       test1.method2();
    }
}
            

Sample Code with Tracing Inserted

Insert traces in the program ( the explanation follows before the example code ).

package com.sap.fooPackage;
import com.sap.tc.logging.*;
 public class FooClass {
   //  (1) The current Java class is defined as a location source.
   private static Location myLoc =
      Location.getLocation("com.sap.fooPackage.FooClass");
   
 method1() {
   //  (2) Enable the writing of trace messages.
      myLoc.entering("method1");
   //  (3) Assigning severity to the trace message - WARNING.
      … … … myLoc.warningT("method1", "Sample warning message");
      … … … myLoc.exiting();
   }
   public void method2() {
      myLoc.entering("method2");
      try {
         … ….} catch (IOException e) {
         myLoc.throwing("method2", e);
      }
      myLoc.exiting();
   }
   public static void main(String[] args) {
      FooClass test1 = new FooClass();
      test1.method1();
      test1.method2();
   }
}
            

There are a few additional lines you need to activate the tracing feature. Refer the numbering below to that in the sample source code.

  • (1) - Get an access to a location object for the current class. A class method is provided by the Location class.

    Note

    You do not have to work on the constructor, just call Location.getLocation(<name of the location object>) and receive a handle to the unique location.

  • (2) - Insert trace message in your code.

  • (3) - Assign a severity (WARNING) to the trace message.

    This severity is equal to the default log controller severity so the message will be logged.

The example shows a few APIs that write out trace messages: entering , warningT , throwing , exiting .

The Output

The messages are directed to the console via the default TraceFormatter , which is human-readable.

The content of the output file looks like this:

May 3, 2007 6:54:18 PM com.sap.fooPackage.FooClass.method1 [main]

Path: Entering method

May 3, 2007 6:54:18 PM com.sap.fooPackage.FooClass.method1 [main]

Warning: Sample warning message

May 3, 2007 6:54:18 PM com.sap.fooPackage.FooClass.method1 [main]

Path: Exiting method

May 3, 2007 6:54:18 PM com.sap.fooPackage.FooClass.method2 [main]

Path: Entering method

May 3, 2007 6:54:18 PM com.sap.fooPackage.FooClass.method2 [main]

Warning: Throwing java.io.FileNotFoundException:

C:\Not_Exist\zzzzz.log (The system cannot find the path specified)

May 3, 2007 6:54:18 PM com.sap.fooPackage.FooClass.method2 [main]

Path: Exiting method

More Information

Coding Recommendations