mazz.i18n
Class Logger

java.lang.Object
  extended by mazz.i18n.Logger
Direct Known Subclasses:
CommonsLogger, JDKLogger, Log4jLogger

public abstract class Logger
extends java.lang.Object

Superclass to all the loggers that provide the ability to log messages that are retrieved from a resource bundle with the additional capability of being able to set the base bundle name and locale that this logger will use to log the message. Note that if this object is created with a null or unspecified locale, this logger will use LoggerLocale to determine what locale it should use.

This object also provides a convienent way to obtain I18N messages from the resource bundle this logger uses in the locale this logger uses. This is useful for when you need to output messages outside of any log mechanism (e.g. creating exceptions with a message string). See getMsg(String, Object[]) and getMsgString(String, Object[]).

This abstraction is to allow subclasses to utilize different logging implementations, like log4j, commons logging, JDK logging, etc. Subclasses should/must override all constructors and simply call the super constructors. Subclasses must implement the createLogObject(String)/createLogObject(Class) methods in order to create its own log implementation object. That created log object should then be used by the subclass' implementations of the different log methods (e.g. fatal(String, Object[]), etc).

By default, all loggers will not dump stack traces to the log when throwables are logged. To change this default behavior, set the system property SYSPROP_DUMP_STACK_TRACES to true or programatically call setDumpStackTraces(boolean) (bear in mind that this will affect all loggers - those that currently exist and those created in the future). Note, however, that this flag has no bearing on Throwables logged at the FATAL level; that is to say, all Throwables that are logged at the FATAL level will always have their stack traces dumped to the log. Additionally, all loggers will dump the resource bundle keys that map to the messages being logged (this is to make it easier to correlate a log message to where it was logged in the code if the message itself is in a language that cannot be read by the person looking at the log). To change this default behavior, set the system property SYSPROP_DUMP_LOG_KEYS to false or programatically call setDumpLogKeys(boolean). Again, this affects all loggers.

Version:
$Revision: 1.2 $
Author:
John Mazzitelli
See Also:
Msg, LoggerLocale

Field Summary
static java.lang.String SYSPROP_DUMP_LOG_KEYS
          System property name that is used to determine if the resource keys are logged along with the log message.
static java.lang.String SYSPROP_DUMP_STACK_TRACES
          System property name that is used to define the default dump-stack-trace behavior.
 
Constructor Summary
Logger(java.lang.Class clazz)
          Creates a new Logger object using a default resource bundle and the default log locale as determined by LoggerLocale.
Logger(java.lang.Class clazz, java.util.Locale locale)
          Creates a new Logger object using a default resource bundle.
Logger(java.lang.Class clazz, Msg.BundleBaseName basename)
          Creates a new Logger object using the default log locale as determined by LoggerLocale.
Logger(java.lang.Class clazz, Msg.BundleBaseName basename, java.util.Locale locale)
          Creates a new Logger object.
Logger(java.lang.String name)
          Creates a new Logger object using a default resource bundle and the default log locale as determined by LoggerLocale.
Logger(java.lang.String name, java.util.Locale locale)
          Creates a new Logger object using a default resource bundle.
Logger(java.lang.String name, Msg.BundleBaseName basename)
          Creates a new Logger object using the default log locale as determined by LoggerLocale.
Logger(java.lang.String name, Msg.BundleBaseName basename, java.util.Locale locale)
          Creates a new Logger object.
 
Method Summary
protected abstract  void createLogObject(java.lang.Class clazz)
          Creates the actual log object to be used by this logger based on the given class.
protected abstract  void createLogObject(java.lang.String name)
          Creates the actual log object to be used by this logger given the name of the logger as a String.
abstract  Msg debug(java.lang.String key, java.lang.Object... varargs)
          Logs the resource bundle message and args.
abstract  Msg debug(java.lang.Throwable throwable, java.lang.String key, java.lang.Object... varargs)
          Logs the throwable with the resource bundle message and args.
abstract  Msg error(java.lang.String key, java.lang.Object... varargs)
          Logs the resource bundle message and args.
abstract  Msg error(java.lang.Throwable throwable, java.lang.String key, java.lang.Object... varargs)
          Logs the throwable with the resource bundle message and args.
abstract  Msg fatal(java.lang.String key, java.lang.Object... varargs)
          Logs the resource bundle message and args.
abstract  Msg fatal(java.lang.Throwable throwable, java.lang.String key, java.lang.Object... varargs)
          Logs the throwable with the resource bundle message and args.
 Msg.BundleBaseName getBaseBundleName()
          Returns the base bundle name of the resource bundle that will house this logger's messages.
static boolean getDumpLogKeys()
          Gets the flag this class will use to determine if the resource bundle key names should be logged along with the log message itself.
static boolean getDumpStackTraces()
          Gets the flag this class will use to determine if stack traces should be logged when Throwables are provided to the log methods.
 java.util.Locale getLocale()
          Returns the locale of the messages that are to be logged.
 Msg getMsg(java.lang.String key, java.lang.Object... varargs)
          Returns a localized Msg object whose message is found in this logger's resource bundle in the logger's locale.
 java.lang.String getMsgString(java.lang.String key, java.lang.Object... varargs)
          Returns a localized message string that is found in this logger's resource bundle in the logger's locale.
abstract  Msg info(java.lang.String key, java.lang.Object... varargs)
          Logs the resource bundle message and args.
abstract  Msg info(java.lang.Throwable throwable, java.lang.String key, java.lang.Object... varargs)
          Logs the throwable with the resource bundle message and args.
abstract  boolean isDebugEnabled()
          Returns true if the debug log level is enabled
abstract  boolean isErrorEnabled()
          Returns true if the error log level is enabled
abstract  boolean isFatalEnabled()
          Returns true if the fatal log level is enabled
abstract  boolean isInfoEnabled()
          Returns true if the info log level is enabled
abstract  boolean isTraceEnabled()
          Returns true if the trace log level is enabled
abstract  boolean isWarnEnabled()
          Returns true if the warn log level is enabled
static void setDumpLogKeys(boolean flag)
          Sets the flag this class will use to determine if the resource bundle key names should be logged along with the log message itself.
static void setDumpStackTraces(boolean flag)
          Sets the flag this class will use to determine if stack traces should be logged when Throwables are provided to the log methods.
abstract  Msg trace(java.lang.String key, java.lang.Object... varargs)
          Logs the resource bundle message and args.
abstract  Msg trace(java.lang.Throwable throwable, java.lang.String key, java.lang.Object... varargs)
          Logs the throwable with the resource bundle message and args.
abstract  Msg warn(java.lang.String key, java.lang.Object... varargs)
          Logs the resource bundle message and args.
abstract  Msg warn(java.lang.Throwable throwable, java.lang.String key, java.lang.Object... varargs)
          Logs the throwable with the resource bundle message and args.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SYSPROP_DUMP_STACK_TRACES

public static final java.lang.String SYSPROP_DUMP_STACK_TRACES
System property name that is used to define the default dump-stack-trace behavior.

See Also:
Constant Field Values

SYSPROP_DUMP_LOG_KEYS

public static final java.lang.String SYSPROP_DUMP_LOG_KEYS
System property name that is used to determine if the resource keys are logged along with the log message.

See Also:
Constant Field Values
Constructor Detail

Logger

public Logger(java.lang.String name,
              Msg.BundleBaseName basename,
              java.util.Locale locale)
Creates a new Logger object.

Parameters:
name - the name to give to the logger (this is usually a class name of the object that will be logging messages)
basename - the base bundle name used to identify the resource bundle to use (may be null)
locale - the locale of the messages that will be logged (may be null)
See Also:
Msg.Msg(Msg.BundleBaseName, Locale)

Logger

public Logger(java.lang.Class clazz,
              Msg.BundleBaseName basename,
              java.util.Locale locale)
Creates a new Logger object.

Parameters:
clazz - the class of the object that will be logging messages
basename - the base bundle name used to identify the resource bundle to use (may be null)
locale - the locale of the messages that will be logged (may be null)
See Also:
Msg.Msg(Msg.BundleBaseName, Locale)

Logger

public Logger(java.lang.String name,
              Msg.BundleBaseName basename)
Creates a new Logger object using the default log locale as determined by LoggerLocale.

Parameters:
name - the name to give to the logger (this is usually a class name of the object that will be logging messages)
basename - the base bundle name used to identify the resource bundle to use (may be null)
See Also:
Msg.Msg(Msg.BundleBaseName)

Logger

public Logger(java.lang.Class clazz,
              Msg.BundleBaseName basename)
Creates a new Logger object using the default log locale as determined by LoggerLocale.

Parameters:
clazz - the class of the object that will be logging messages
basename - the base bundle name used to identify the resource bundle to use (may be null)
See Also:
Msg.Msg(Msg.BundleBaseName)

Logger

public Logger(java.lang.String name,
              java.util.Locale locale)
Creates a new Logger object using a default resource bundle.

Parameters:
name - the name to give to the logger (this is usually a class name of the object that will be logging messages)
locale - the locale of the messages that will be logged (may be null)
See Also:
Msg.Msg(Locale)

Logger

public Logger(java.lang.Class clazz,
              java.util.Locale locale)
Creates a new Logger object using a default resource bundle.

Parameters:
clazz - the class of the object that will be logging messages
locale - the locale of the messages that will be logged (may be null)
See Also:
Msg.Msg(Locale)

Logger

public Logger(java.lang.String name)
Creates a new Logger object using a default resource bundle and the default log locale as determined by LoggerLocale.

Parameters:
name - the name to give to the logger (this is usually a class name of the object that will be logging messages)
See Also:
Msg.Msg()

Logger

public Logger(java.lang.Class clazz)
Creates a new Logger object using a default resource bundle and the default log locale as determined by LoggerLocale.

Parameters:
clazz - the class of the object that will be logging messages
See Also:
Msg.Msg()
Method Detail

getDumpStackTraces

public static boolean getDumpStackTraces()
Gets the flag this class will use to determine if stack traces should be logged when Throwables are provided to the log methods.

Returns:
flag to indicate if stack traces are dumped to the log

setDumpStackTraces

public static void setDumpStackTraces(boolean flag)
Sets the flag this class will use to determine if stack traces should be logged when Throwables are provided to the log methods.

Parameters:
flag - the new flag to indicate if stack traces are to be dumped to the log

getDumpLogKeys

public static boolean getDumpLogKeys()
Gets the flag this class will use to determine if the resource bundle key names should be logged along with the log message itself. When enabled, this helps a person looking at the log messages to more easily determine what a log message is and what code generated it if that person does not understand the language that the log message was written in.

Returns:
flag to indicate if resource bundle keys are logged along with the log message

setDumpLogKeys

public static void setDumpLogKeys(boolean flag)
Sets the flag this class will use to determine if the resource bundle key names should be logged along with the log message itself. When this is set to true, this helps a person looking at the log messages to more easily determine what a log message is and what code generated it if that person does not understand the language that the log message was written in.

Parameters:
flag - the new flag to indicate if resource bundle keys are logged along with the log message

getBaseBundleName

public Msg.BundleBaseName getBaseBundleName()
Returns the base bundle name of the resource bundle that will house this logger's messages.

Returns:
base bundle name; if null, the default base bundle name will be used
See Also:
Msg.getBundleBaseNameDefault()

getLocale

public java.util.Locale getLocale()
Returns the locale of the messages that are to be logged. If this object was created with a null or unspecified locale, this method will use LoggerLocale to determine what locale to return.

Returns:
locale that this logger will use to log messages

getMsg

public Msg getMsg(java.lang.String key,
                  java.lang.Object... varargs)
Returns a localized Msg object whose message is found in this logger's resource bundle in the logger's locale.

Parameters:
key - the resource bundle key of the message to get
varargs - the resource bundle message arguments
Returns:
the localized message object
See Also:
getMsgString(String, Object[])

getMsgString

public java.lang.String getMsgString(java.lang.String key,
                                     java.lang.Object... varargs)
Returns a localized message string that is found in this logger's resource bundle in the logger's locale. This is equivalent to getMsg(String, Object[]).toString().

Parameters:
key - the resource bundle key of the message to get
varargs - the resource bundle message arguments
Returns:
the localized message string
See Also:
getMsg(String, Object[])

isFatalEnabled

public abstract boolean isFatalEnabled()
Returns true if the fatal log level is enabled

Returns:
enabled flag

isErrorEnabled

public abstract boolean isErrorEnabled()
Returns true if the error log level is enabled

Returns:
enabled flag

isWarnEnabled

public abstract boolean isWarnEnabled()
Returns true if the warn log level is enabled

Returns:
enabled flag

isInfoEnabled

public abstract boolean isInfoEnabled()
Returns true if the info log level is enabled

Returns:
enabled flag

isDebugEnabled

public abstract boolean isDebugEnabled()
Returns true if the debug log level is enabled

Returns:
enabled flag

isTraceEnabled

public abstract boolean isTraceEnabled()
Returns true if the trace log level is enabled

Returns:
enabled flag

fatal

public abstract Msg fatal(java.lang.Throwable throwable,
                          java.lang.String key,
                          java.lang.Object... varargs)
Logs the throwable with the resource bundle message and args.

Parameters:
throwable - the throwable to log
key - the resource bundle key of the message to log
varargs - the resource bundle message arguments
Returns:
the resource bundle message that was logged

fatal

public abstract Msg fatal(java.lang.String key,
                          java.lang.Object... varargs)
Logs the resource bundle message and args.

Parameters:
key - the resource bundle key of the message to log
varargs - the resource bundle message arguments
Returns:
the resource bundle message that was logged

error

public abstract Msg error(java.lang.Throwable throwable,
                          java.lang.String key,
                          java.lang.Object... varargs)
Logs the throwable with the resource bundle message and args.

Parameters:
throwable - the throwable to log
key - the resource bundle key of the message to log
varargs - the resource bundle message arguments
Returns:
the resource bundle message that was logged

error

public abstract Msg error(java.lang.String key,
                          java.lang.Object... varargs)
Logs the resource bundle message and args.

Parameters:
key - the resource bundle key of the message to log
varargs - the resource bundle message arguments
Returns:
the resource bundle message that was logged

warn

public abstract Msg warn(java.lang.Throwable throwable,
                         java.lang.String key,
                         java.lang.Object... varargs)
Logs the throwable with the resource bundle message and args.

Parameters:
throwable - the throwable to log
key - the resource bundle key of the message to log
varargs - the resource bundle message arguments
Returns:
the resource bundle message that was logged

warn

public abstract Msg warn(java.lang.String key,
                         java.lang.Object... varargs)
Logs the resource bundle message and args.

Parameters:
key - the resource bundle key of the message to log
varargs - the resource bundle message arguments
Returns:
the resource bundle message that was logged

info

public abstract Msg info(java.lang.Throwable throwable,
                         java.lang.String key,
                         java.lang.Object... varargs)
Logs the throwable with the resource bundle message and args.

Parameters:
throwable - the throwable to log
key - the resource bundle key of the message to log
varargs - the resource bundle message arguments
Returns:
the resource bundle message that was logged

info

public abstract Msg info(java.lang.String key,
                         java.lang.Object... varargs)
Logs the resource bundle message and args.

Parameters:
key - the resource bundle key of the message to log
varargs - the resource bundle message arguments
Returns:
the resource bundle message that was logged

debug

public abstract Msg debug(java.lang.Throwable throwable,
                          java.lang.String key,
                          java.lang.Object... varargs)
Logs the throwable with the resource bundle message and args.

Parameters:
throwable - the throwable to log
key - the resource bundle key of the message to log
varargs - the resource bundle message arguments
Returns:
the resource bundle message that was logged

debug

public abstract Msg debug(java.lang.String key,
                          java.lang.Object... varargs)
Logs the resource bundle message and args.

Parameters:
key - the resource bundle key of the message to log
varargs - the resource bundle message arguments
Returns:
the resource bundle message that was logged

trace

public abstract Msg trace(java.lang.Throwable throwable,
                          java.lang.String key,
                          java.lang.Object... varargs)
Logs the throwable with the resource bundle message and args.

Parameters:
throwable - the throwable to log
key - the resource bundle key of the message to log
varargs - the resource bundle message arguments
Returns:
the resource bundle message that was logged

trace

public abstract Msg trace(java.lang.String key,
                          java.lang.Object... varargs)
Logs the resource bundle message and args.

Parameters:
key - the resource bundle key of the message to log
varargs - the resource bundle message arguments
Returns:
the resource bundle message that was logged

createLogObject

protected abstract void createLogObject(java.lang.Class clazz)
Creates the actual log object to be used by this logger based on the given class.

Parameters:
clazz - the class to associate with the log object that is to be created

createLogObject

protected abstract void createLogObject(java.lang.String name)
Creates the actual log object to be used by this logger given the name of the logger as a String.

Parameters:
name - the name to associate with the log object that is to be created


Copyright © 2006 John J. Mazzitelli All Rights Reserved. Licensed under LGPL