結合モードで 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 のロジック候補の 1 つが 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);