以组合方式使用 JRas 操作来将跟踪数据以及日志记录消息输出至 WebSphere® Application Server 和用户定义的日志。
开始之前
不推荐使用本任务及其子任务中描述的 JRas 框架。但是,可以使用 Java™ 记录来获得类似结果。
关于此任务
组合方式中,消息和跟踪记录到 WebSphere Application Server 日志和用户定义的日志中。下列样本假设:
- 您编写了名为 SimpleFileHandler 的用户定义的处理程序和名为 SimpleFormatter 的用户定义的格式化程序。
- 您不使用用户定义的类型或事件。
过程
- 导入必要的 JRas 扩展类:
import com.ibm.ras.*;
import com.ibm.websphere.ras.*;
- 导入用户处理程序和格式化程序:
import com.ibm.ws.ras.test.user.*;
- 声明记录器引用:
private RASMessageLogger msgLogger = null;
private RASTraceLogger trcLogger = null;
- 获取到管理器类的引用、创建记录器并添加用户处理程序。 因为记录器是已命名的单元素集合,所以您可在许多地方获取对记录器的引用。企业 bean 的一个逻辑候选是 ejbCreate 方法。确保不会意外地将同一个用户处理程序的多个实例插入到同一个记录器。您的初始化代码必须支持此方法。以下样本是消息记录器样本。对于跟踪记录器,过程是类似的。
com.ibm.websphere.ras.Manager mgr = com.ibm.websphere.ras.Manager.getManager();
msgLogger = mgr.createRASMessageLogger("Acme", "WidgetCounter", "RasTest",
myTestBean.class.getName());
// Configure the message logger to use the message file defined
// in the ResourceBundle sample.
msgLogger.setMessageFile("acme.widgets.DefaultMessages");
// Create the user handler and formatter. Configure the formatter,
// then add it to the handler.
RASIHandler handler = new SimpleFileHandler("myHandler", "FileName");
RASIFormatter formatter = new SimpleFormatter("simple formatter");
formatter.addEventClass("com.ibm.ras.RASMessageEvent");
handler.addFormatter(formatter);
// Add the Handler to the logger. Add the logger to the list of the
// handlers listeners, then set the handlers
// mask, which updates the loggers composite mask appropriately.
// WARNING - there is an order dependency here that must be followed.
msgLogger.addHandler(handler);
handler.addMaskChangeListener(msgLogger);
handler.setMessageMask(RASIMessageEvent.DEFAULT_MESSAGE_MASK);