The Trace object allows the Java program to log trace points and diagnostic messages. This information helps reproduce and diagnose problems.
The Trace class logs the following categories of information:
Conversion | Logs character set conversions between Unicode and native code pages. This category should be used only by the AS/400 Toolbox for Java classes. |
---|---|
Information | Traces the flow through a program. |
Warning | Logs information about errors the program was able to recover from. |
Error | Logs additional errors that cause an exception. |
Diagnostic | Logs state information. |
Data stream | Logs the data that flows between the AS/400 and the Java program. This category should be used only by the AS/400 Toolbox for Java classes. |
Proxy | This category is used by AS/400 Toolbox for Java classes to log data flow between the client and the proxy server. |
All | This category is used to enable or disable tracing for all of the above categories at once. Trace information can not be directly logged to this category. |
The AS/400 Toolbox for Java classes also use the trace categories. When a Java program enables logging, AS/400 Toolbox for Java information is included with the information that is recorded by the application.
You can enable the trace for a single category or a set of categories. Once the categories are selected, use the setTraceOn method to turn tracing on and off. Data is written to the log using the log method.
Excessive logging can impact performance. Use the isTraceOn method to query the current state of the trace. Your Java program can use this method to determine whether it should build the trace record before it calls the log method. Calling the log method when logging is off is not an error, but it takes more time.
The default is to write log information to standard out. To redirect the log to a file, call the setFileName() method from your Java application. In general, this works only for Java applications because most browsers do not give applets access to write to the local file system.
Logging is off by default. Java programs should provide a way for the user to turn on logging so that it is easy to enable logging. For example, the application can parse for a command line parameter that indicates which category of data should be logged. The user can set this parameter when log information is needed.
The following examples show how to use the Trace class.
Example 1: The following is an example of how to use the setTraceOn method, and how to write data to a log by using the log method.
// Enable diagnostic, information, and warning logging. Trace.setTraceDiagnosticOn(true); Trace.setTraceInformationOn(true); Trace.setTraceWarningOn(true); // Turning tracing on. Trace.setTraceOn(true); // ... At this point in the Java program, // write to the log. Trace.log(Trace.INFORMATION, "Just entered class xxx, method xxx"); // Turning tracing off. Trace.setTraceOn(false);
Example 2: The following examples show how to use trace. Method 2 is the preferable way to write code that uses trace.
// Method 1 - build a trace record // then call the log method and let // the trace class determine if the // data should be logged. This will // work but will be slower than the // following code. String traceData = new String("just entered class xxx, data = "); traceData = traceData + data + "state = " + state; Trace.log(Trace.INFORMATION, traceData); // Method 2 - check the log status // before building the information to // log. This is faster when tracing // is not active. if (Trace.isTraceOn() && Trace.isTraceInformationOn()) { String traceData = new String("just entered class xxx, data = "); traceData = traceData + data + "state = " + state; Trace.log(Trace.INFORMATION, traceData); }
[ Information Center Home Page | Feedback ] | [ Legal | AS/400 Glossary ] |