跟踪类
Trace 类允许 Java™ 程序记录跟踪点和诊断消息。 此信息有助于重现和诊断问题。
注: 您还可以使用 跟踪系统属性来设置跟踪。
Trace 类记录以下类别的信息:
| information category(信息类别) | 描述 |
|---|---|
| 转换 | 记录 Unicode 与代码页之间的字符集转换。 此类别仅由 IBM® Toolbox for Java 类使用。 |
| 数据流 | 记录在系统和 Java 程序之间流动的数据。 此类别仅由 IBM Toolbox for Java 类使用。 |
| 诊断 | 记录状态信息。 |
| 错误 | 记录导致异常的其他错误。 |
| 信息 | 跟踪通过程序的流。 |
| PCML | 此类别用于确定 PCML 如何解释发送到服务器和从服务器发送的数据。 |
| 代理 | IBM Toolbox for Java 类使用此类别来记录客户机与代理服务器之间的数据流。 |
| 警告 | 记录有关程序能够从中恢复的错误的信息。 |
| 全部 | 此类别用于同时对上述所有类别启用或禁用跟踪。 无法将跟踪信息直接记录到此类别。 |
IBM Toolbox for Java 类也使用跟踪类别。 当 Java 程序启用日志记录时, IBM Toolbox for Java 信息包含在应用程序记录的信息中。
您可以对单个类别或一组类别启用跟踪。 选择类别后,使用 setTraceOn 方法打开或关闭跟踪。 使用 log 方法将数据写入日志。
您可以将不同组件的跟踪数据发送到不同的日志。 缺省情况下,跟踪数据将写入缺省日志。 使用组件跟踪将特定于应用程序的跟踪数据写入单独的日志或标准输出。 通过使用组件跟踪,您可以轻松地将特定应用程序的跟踪数据与其他数据分开。
过多的日志记录会影响性能。 使用 isTraceOn 方法查询跟踪的当前状态。 Java 程序可以使用此方法来确定它在调用日志方法之前是否构建跟踪记录。 在关闭日志记录时调用 log 方法不是错误,但需要更多时间。
缺省值是将日志信息写入标准输出。 要将日志重定向到文件,请调用 Java 应用程序中的 setFileName() 方法。 通常,这仅适用于 Java 应用程序,因为大多数浏览器不会授予 applet 访问权以写入本地文件系统。
缺省情况下,日志记录处于关闭状态。 Java 程序为用户提供了开启日志记录的方法,以便易于启用日志记录。 例如,应用程序可以解析命令行参数,该参数指示记录的数据类别。 当需要日志信息时,用户可以设置此参数。
示例
注: 请阅读 代码示例免责声明 以获取重要的法律信息。
以下示例显示如何使用 Trace 类。
示例 使用 setTraceOn() 并通过使用 log 方法将数据写入日志
// Enable diagnostic, information, and warning logging.
Trace.setTraceDiagnosticOn(true);
Trace.setTraceInformationOn(true);
Trace.setTraceWarningOn(true);
// Turn 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);示例: 使用跟踪
在以下代码中,方法 2 是使用跟踪的首选方法。
// 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);
}示例: 使用组件跟踪
// Create a component string. It is more efficient to create an
// object than many String literals.
String myComponent1 = "com.myCompany.xyzComponent";
String myComponent2 = "com.myCompany.abcComponent";
// Send IBM Toolbox for Java and the component trace data each to separate files.
// The trace will contain all trace information, while each
// component log file will only contain trace information specific to
// that component. If a Trace file is not specified, all trace data
// will go to standard out with the component specified in front of
// each trace message.
// Trace.setFileName("c:\\bit.bucket");
// Trace.setFileName(myComponent1, "c:\\Component1.log");
// Trace.setFileName(myComponent2, "c:\\Component2.log");
Trace.setTraceOn(true); // Turn trace on.
Trace.setTraceInformationOn(true); // Enable information messages.
// Log component specific trace data or general IBM Toolbox for Java
// trace data.
Trace.setFileName("c:\\bit.bucket");
Trace.setFileName(myComponent1, "c:\\Component1.log");