プライマリ・コンテンツに移動
Oracle® Fusion Middleware Oracle WebLogic ServerにデプロイされたアプリケーションへのWebLogicロギング・サービスの追加
12c (12.2.1.3.0)
E90338-01
目次へ移動
目次

前

D WebLogic Server用のLoggerクラスのリファレンス

Loggerクラスは、WebLogic Serverロギングのインタフェースを提供します。 この付録では、Loggerクラスについて説明し、メッセージ・カタログとそれに対応するLoggerクラスの例を示します。

Loggerクラスについて

i18ngenによって生成されるクラスは、Loggerと呼ばれています。Loggerクラスは、WebLogicロギングのインタフェースを提供します。カタログXyz.xmlの場合、LoggerクラスXyzLoggerが生成されます。

Loggerクラスは、カタログに定義されたすべてのメッセージをWebLogic Serverログに記録するメソッドを提供します。組み込まれているメソッドは、関連するカタログで定義されたものと同じです。カタログでloggables属性がtrueに指定されていると、メッセージごとにLoggableメソッドが併せて生成されます。「WebLogic Server用のLoggableオブジェクトのリファレンス」を参照してください。

生成されたLoggerクラスの例

weblogic.i18ngenユーティリティを使用して、ログ・メッセージ内のテキストのローカライズに使用されるLoggerクラスを生成できます。生成されたLoggerクラスの例を例D-1に示します。

例D-1 生成されたLoggerクラスの例

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE message_catalog PUBLIC "weblogic-message-catalog-dtd" 
"http://www.bea.com/servers/wls90/dtd/msgcat.dtd">
<message_catalog
   i18n_package="examples.i18n.logging"
   l10n_package="examples.i18n.logging"
   subsystem="I18N"
   version="1.0"
   baseid="600000"
   endid="610000"
   loggables="true"
   >
   <logmessage
     messageid="600000"
     method="logEntry()"
     severity="info"
     >
     <messagebody>Starting I18nLog example...</messagebody>
     <messagedetail></messagedetail>
     <cause></cause>
     <action></action>
   </logmessage>
   <logmessage
     messageid="600001"
     method="testArgs(String name,int cnt)"
     severity="debug"
     >
     <messagebody>Class {0} started with {1,number} arguments.</messagebody>
     <messagedetail></messagedetail>
     <cause></cause>
     <action></action>
   </logmessage>
   <logmessage
     messageid="600002"
     method="logTrace(Throwable t)"
     severity="error"
     stacktrace="true"
     >
     <messagebody>This message is followed by a trace</messagebody>
     <messagedetail></messagedetail>
     <cause></cause>
     <action></action>
   </logmessage>
   <logmessage
     messageid="600003"
     method="logNoTrace(Throwable t)"
     severity="warning"
     stacktrace="false"
     >
     <messagebody>This message is not followed by a trace, but we can insert its text : {0}</messagebody>
     <messagedetail></messagedetail>
     <cause></cause>
     <action></action>
   </logmessage>
   <logmessage
     messageid="600004"
     method="getId()"
     severity="info"
     >
     <messagebody>This message's id will be in the next message</messagebody>
     <messagedetail>A message can contain additional detailed information.</messagedetail>
     <cause>This message is displayed on purpose</cause>
     <action>Nothing to do, the example is working</action>
   </logmessage>
   <logmessage
     messageid="600005"
     method="showId(String id)"
     severity="info"
     >
     <messagebody>The previous message logged had message id {0}</messagebody>
     <messagedetail></messagedetail>
     <cause></cause>
     <action></action>
   </logmessage>
</message_catalog>

例D-2は、weblogic.i18ngenが生成した、これに対応するJavaソース・コードを示しています。

例D-2 生成されたLoggerクラスの例

package examples.i18n.logging;

import weblogic.logging.MessageLogger;
import weblogic.logging.Loggable;
import java.util.MissingResourceException;
public class I18nLogLogger
{
  /**
   * Starting I18nLog example...
   * @exclude
   *
   * messageid:  600000
   * severity:   info
   */
  public static String logEntry()  {
    Object [] args = {  };
    MessageLogger.log(
        "600000",
        args,
        "examples.i18n.logging.I18nLogLogLocalizer");
    return "600000";
  }
  public static Loggable logEntryLoggable() throws MissingResourceException {
    Object[] args = {  };
    return new Loggable("600000", args);
  }
  /**
   * Class {0} started with {1,number} arguments.
   * @exclude
   *
   * messageid:  600001
   * severity:   debug
   */
  public static String testArgs(String arg0, int arg1)  {
    Object [] args = { arg0, new Integer(arg1) };
    MessageLogger.log(
        "600001",
        args,
        "examples.i18n.logging.I18nLogLogLocalizer");
    return "600001";
  }
  public static Loggable testArgsLoggable(String arg0, int arg1) throws MissingResourceException {
    Object[] args = { arg0, new Integer(arg1) };
    return new Loggable("600001", args);
  }
  /**
   * This message is followed by a trace
   * @exclude
   *
   * messageid:  600002
   * severity:   error
   */
  public static String logTrace(Throwable arg0)  {
    Object [] args = { arg0 };
    MessageLogger.log(
        "600002",
        args,
        "examples.i18n.logging.I18nLogLogLocalizer");
    return "600002";
  }
  public static Loggable logTraceLoggable(Throwable arg0) throws MissingResourceException {
    Object[] args = { arg0 };
    return new Loggable("600002", args);
  }
  /**
   * This message is not followed by a trace, but we can insert its text : {0}
   * @exclude
   *
   * messageid:  600003
   * severity:   warning
   */
  public static String logNoTrace(Throwable arg0)  {
    Object [] args = { arg0 };
    MessageLogger.log(
        "600003",
        args,
        "examples.i18n.logging.I18nLogLogLocalizer");
    return "600003";
  }
  public static Loggable logNoTraceLoggable(Throwable arg0) throws MissingResourceException {
    Object[] args = { arg0 };
    return new Loggable("600003", args);
  }
  /**
   * This message's id will be in the next message
   * @exclude
   *
   * messageid:  600004
   * severity:   info
   */
  public static String getId()  {
    Object [] args = {  };
    MessageLogger.log(
        "600004",
        args,
        "examples.i18n.logging.I18nLogLogLocalizer");
    return "600004";
  }
  public static Loggable getIdLoggable() throws MissingResourceException {
    Object[] args = {  };
    return new Loggable("600004", args);
  }
  /**
   * The previous message logged had message id {0}
   * @exclude
   *
   * messageid:  600005
   * severity:   info
   */
  public static String showId(String arg0)  {
    Object [] args = { arg0 };
    MessageLogger.log(
        "600005",
        args,
        "examples.i18n.logging.I18nLogLogLocalizer");
    return "600005";
  }
  public static Loggable showIdLoggable(String arg0) throws MissingResourceException {
    Object[] args = { arg0 };
    return new Loggable("600005", args);
  }

}

例D-3は、weblogic.i18nLog(国際化(I18n)ロギング・インタフェース)を使用するサンプル・アプリケーションを示しています。この例では、通知メッセージがログとして記録されます。

例D-3 i18nLogを使用するアプリケーション例

package examples.i18n.logging;

import java.util.Locale;

import weblogic.i18n.Localizer;
import weblogic.i18ntools.L10nLookup;
import weblogic.logging.Loggable;

/**
 * This example shows how to use the internationalized (I18n) logging interfaces.
 * <p>
 * usage: java examples.i18n.logging.I18nLog
 * <p>
 * Build procedure: run bld.sh (UNIX) or bld.cmd (NT). These scripts
 * process the I18nLog.xml catalog, producing the logging class, 
 * <tt>examples.i18n.logging.I18nLogLogger</tt>. This class contains static 
 * methods for logging messages to the WLS server log. The methods 
 * and arguments are defined in the I18nLog.xml catalog. This example also 
 * uses a simple message catalog, I18nSimple.xml.
 */


public  class I18nLog {

    public I18nLog() {}

    public static void main(String[] argv) {
      /**
       * This call just logs an info message. There are no arguments defined
       * for this method. 
       *
       * This also shows how to use the Loggable form of the method.
       */

      Loggable ll = I18nLogLogger.logEntryLoggable();
      ll.log();
      System.out.println(ll.getMessage());

      /**
       * Here's an example of a message including a variety
       * of arguments.
       */
      I18nLogLogger.testArgs(I18nLog.class.getName(),argv.length);
      /**
       * If a Throwable is passed then it will result in a stack trace
       * being logged along with the method by default.
       */
      Throwable t = new Throwable("Test with stack trace");
      I18nLogLogger.logTrace(t);
      /** 
       * Messages can optionally be defined to not log a stack trace.
       */
      I18nLogLogger.logNoTrace(t);
      /**
       * The logger methods return the message id for applications
       * that want to do more than just log these messages.
       */
      String messageId = I18nLogLogger.getId();
      I18nLogLogger.showId(messageId);
      /**
       * The message id can be used to obtain the different attributes
       * of a message. The L10nLookup object provides access to the catalogs
       * via Localizer classes. Localizers provide the access to individual
       * messages. Each log message catalog has two Localizers: one for
       * general message information and one for the detailed attributes.
       *
       * The basic Localizer provides access to catalog information:
       *     Version
       *     L10n Package - package for catalog data
       *     I18n Package - package for Logger methods
       *     Subsystem - catalog subsystem
       * For each message it also provides:
       *     Severity: debug, info, warning, error
       *     Message Body - the message text
       *     Stack option - whether to log a stack trace
       *     
       * First get to the L10nLookup properties, then use them to get the
       * Localizers for the message.
       */
      L10nLookup l10n = L10nLookup.getL10n(); 
      /**
       * This returns the basic Localizer (arg 3 = false)
       */
      Localizer lcl = l10n.getLocalizer(messageId,Locale.getDefault(),false);
      /**
       * This returns the detailed Localizer (arg 3 = true)
       */
      Localizer lclDetail = l10n.getLocalizer(messageId,Locale.getDefault(),true);
      /**
       * Use this appplication's simple message catalog to display the
       * log message catalog information
       */
      I18nSimpleTextFormatter fmt = new I18nSimpleTextFormatter();
      System.out.println(fmt.version(messageId,lcl.getVersion()));
      System.out.println(fmt.l10nPackage(messageId,lcl.getL10nPackage()));
      System.out.println(fmt.i18nPackage(messageId,lcl.getI18nPackage()));
      System.out.println(fmt.subsystem(messageId,lcl.getSubSystem()));
      System.out.println(fmt.severity(messageId,lcl.getSeverity(messageId)));
      System.out.println(fmt.body(messageId,lcl.getBody(messageId)));
      System.out.println(fmt.stack(messageId,lcl.getStackTrace(messageId)));
      /**
       * Now for the detailed information.
       */
      System.out.println(fmt.detail(messageId,lclDetail.getDetail(messageId)));
      System.out.println(fmt.cause(messageId,lclDetail.getCause(messageId)));
      System.out.println(fmt.action(messageId,lclDetail.getAction(messageId)));

    }
}