!--a11y-->
A Simple
Example Flow 
1. Identify the source area and get a handle to a source object: Location.
a. Name the ‘ID’ of the source location object as the complete package path 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 flow with the required severity level.
...
a. The common places are: entering/exiting a method, upon throwing an exception, after performing certain critical tasks, and so on.
b. Decide the severity to be assigned to these trace messages.
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; // (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 // Assign destination, no destination by default static { myLoc.setEffectiveSeverity(Severity.PATH); myLoc.addLog(new ConsoleLog()); } public void method1() { // (3) 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 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) – Assign a severity level and a destination for the location object.
This will be the level to be compared against the severity of the trace messages. Messages with severity lower than that of the location object will not be printed. See Appendix A to become familiar with the severity level definition.
By default, Severity.NONE will be 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.
Also by default, no output destination is assigned (again, no output at all). In the example above, the output is directed to the console. See Appendix B with the class diagram, which shows that we mainly have two types of logs: FileLog, ConsoleLog.

You can configure this setting externally. It is not necessary to hardcode it here in the source. For more information, see Configuration Tool.
· (3) – Insert trace messages in your program.
The example shows a few APIs that write out trace messages: entering, warningT, throwing, exiting. Which messages will eventually be printed? We know that all these messages will successfully pass the level Severity.PATH (as specified in this example in step (2)).
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] |
See also: