1. Identify the source area and get a handle to a source object: Location.
Name the ‘ID’ of the source location object as the complete package path of the class, for example, com.sap.fooPackage.FooClass.
2. Insert messages at points where you want to trace the program flow.
3. Assign a severity to the trace message
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.
A Typical Program 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(); } } |
Insert traces in the program (the explanation follows after the example code snippet).
Sample Program Showing the Instrumentation of Traces
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"); public void 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 mainly 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. The API provided is intuitive enough: 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) – Insert trace message in your program.
● (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 messages are directed to the console with the (default) format that is readable by operators (using TraceFormatter).
The content of the output file looks like this:
May 3, 2001 6:54:18 PM com.sap.fooPackage.FooClass.method1 [main] Path: Entering method May 3, 2001 6:54:18 PM com.sap.fooPackage.FooClass.method1 [main] Warning: Sample warning message May 3, 2001 6:54:18 PM com.sap.fooPackage.FooClass.method1 [main] Path: Exiting method May 3, 2001 6:54:18 PM com.sap.fooPackage.FooClass.method2 [main] Path: Entering method May 3, 2001 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, 2001 6:54:18 PM com.sap.fooPackage.FooClass.method2 [main] |