Logger Class Reference for BEA WebLogic Server
The following sections provide reference information for Logger classes:
About Logger Classes
The classes generated by i18ngen are known as Loggers. Logger classes provide the interface to WebLogic Server error logging. For catalog Xyz.xml, a Logger class XyzLogger is generated.
The Logger class provides methods to log all messages defined in a catalog to the WebLogic Server log. The methods included are the same as those defined in the associated catalog. If the catalog specifies the loggables attribute to be true, then Loggable methods are also generated for each message.
For more information, refer to Loggable Object Reference for BEA WebLogic Server.
Example of a Generated Logger Class
Listing D-1 contains an example of a generated logger class.
Listing D-1 Example of Generated Logger Class
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE message_catalog PUBLIC "weblogic-message-catalog-dtd" "http://www.bea.com/servers/wls810/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>Listing D-2 shows the corresponding Java source generated by weblogic.i18ngen.
Listing D-2 Example of Generated Logger Class
package examples.i18n.logging;
import weblogic.logging.MessageLogger;
import weblogic.logging.Loggable;
import java.util.MissingResourceException;
/**
* Copyright (c) 2001 by BEA Systems, Inc. All Rights Reserved.
* @exclude
*/
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); }
}Listing D-3 shows an example application that uses the weblogic.i18nLog (internationalized (I18n) logging interfaces). The example logs an informational message.
Listing D-3 Example of Application Using i18nLog
package examples.i18n.logging;
import java.util.Locale;
import weblogic.i18n.Localizer;
import weblogic.i18ntools.L10nLookup;
import weblogic.logging.Loggable;
/**
* @author Copyright (c) 2000 by BEA Systems, Inc. All Rights Reserved.
*/
/**
* 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 error 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 messageid 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 (128), info (64), warning (32), error (8) * 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 * Localizer's 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 appplications 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)));
}
}