独自のカスタム・ログ・ファイルを使用する必要がある場合があります。 カスタムのハンドラー、フィルター、およびフォーマッターを追加すると、デフォルトの WebSphere Application Server ロギング・インフラストラクチャーの構成によって実現できる 環境を超えて、ロギング環境をカスタマイズすることができます。
以下の例は、要求を処理するために、新規ハンドラーをロガーの com.myCompany サブツリーへ追加する方法を示したものです (ロガー階層の構成 を参照)。 この例の main メソッドでは、新規に構成されたロガーの使用方法に ついて述べます。
import java.util.Vector; import java.util.logging.Filter; import java.util.logging.Formatter; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; public class MyCustomLogging { public MyCustomLogging() { super(); } public static void initializeLogging() { // Get the logger that you want to attach a custom Handler to String defaultResourceBundleName = "com.myCompany.Messages"; Logger logger = Logger.getLogger("com.myCompany", defaultResourceBundleName); // Set up a custom Handler (see MyCustomHandler example) Handler handler = new MyCustomHandler("MyOutputFile.log"); // Set up a custom Filter (see MyCustomFilter example) Vector acceptableLevels = new Vector(); acceptableLevels.add(Level.INFO); acceptableLevels.add(Level.SEVERE); Filter filter = new MyCustomFilter(acceptableLevels); // Set up a custom Formatter (see MyCustomFormatter example) Formatter formatter = new MyCustomFormatter(); // Connect the filter and formatter to the handler handler.setFilter(filter); handler.setFormatter(formatter); // Connect the handler to the logger logger.addHandler(handler); // avoid sending events logged to com.myCompany showing up in WebSphere // Application Server logs logger.setUseParentHandlers(false); } public static void main(String[] args) { initializeLogging(); Logger logger = Logger.getLogger("com.myCompany"); logger.info("This is a test INFO message"); logger.warning("This is a test WARNING message"); logger.logp(Level.SEVERE, "MyCustomLogging", "main", "This is a test SEVERE message"); } }
C:¥>type MyOutputFile.log Sat Sep 04 11:21:19 EDT 2004 INFO This is a test INFO message Sat Sep 04 11:21:19 EDT 2004 SEVERE This is a test SEVERE message