Show TOC Start of Content Area

This graphic is explained in the accompanying text Sample Java Code with Logging  Locate the document in its SAP Library structure

Use

Proceed as follows:

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

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

       2.      Assign a severity level to the location object.

       3.      Specify an output destination for the location object.

       4.      Insert messages at points where you want to trace the program code with the required severity level.

...

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

       Decide the severity to be assigned to these trace messages.

Activities

A Program without Logging 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();

   }

}

 

A Program with Logging Inserted

package com.sap.fooPackage;

   // (0)

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");

   //  (2) Assign severity, otherwise; nothing will be printed out by default

   //  (3) Specify a destination; no destination by default

   static {

      myLoc.setEffectiveSeverity(Severity.PATH);

      myLoc.addLog(new ConsoleLog());

   }

   public void method1() {

   //  (4) Enable the writing of trace messages

      myLoc.entering("method1");

      myLoc.warningT("method1", "Sample warning message");

      myLoc.exiting();

   }

   public void method2() {

      myLoc.entering("method2");

      try {

         ….} catch (IOException e) {

         myLoc.throwing("method2", e);

         //e.g. file not found exception

         …..

      }

      myLoc.exiting();

   }

   public static void main(String[] args) {

      FooClass test1 = new FooClass();

      test1.method1();

      test1.method2();

   }

}

 

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

      (0) – Import the SAP logging package.

      (1) – Get an access to a location object for the current class. The API provided is intuitive: a class method provided by the Location class. 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) – Assign a severity level and a destination for the location object.

Messages with severity lower than the one of the location object will not be printed. More information about severity levels: Appendix A .

By default, Severity.NONE is assigned (that is, there will be no output at all). So, for this sample coding, an explicit level Severity.PATH is assigned to screen out all the debugging messages that have a lower severity.

      (3) – Specify an output destination. By default, no output destination is assigned . In the example above, the output is directed to the console. See Appendix B with the class diagram, which shows the two main types of logs: FileLog, ConsoleLog.

Note

You can configure this setting externally. It is not necessary to hardcode it here in the source. For more information, see Configuration Tool.

       (4) – Insert trace messages in your program.

The example shows a few APIs that write out trace messages: entering, warningT, throwing, exiting. All these messages will successfully pass the level Severity.PATH (as specified in this example in step (2)).

 

Program Output

The messages are directed to the console in the human-readable format (using TraceFormatter).

The content of the output file looks like this:

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

Path: Entering method

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

          Warning: Sample warning message

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

          Path: Exiting method

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

Path: Entering method

May 3, 2003 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, 2003 6:54:18 PM com.sap.fooPackage.FooClass.method2 [main]   

                                                 Path: Exiting method

 

More information:

Coding Recommendations

              

End of Content Area