FileDocCategorySizeDatePackage
AbstractLogger.javaAPI DocGlassfish v2 API27818Fri May 04 22:35:24 BST 2007com.sun.jdo.spi.persistence.utility.logging

AbstractLogger

public abstract class AbstractLogger extends Object implements Logger
This class provides a default implementation of the com.sun.jdo.spi.persistence.utility.logging.Logger interface which implements most methods and/or delegates them to a few abstract methods which subclasses must override.
author
Rochelle Raccah
version
%I%

Fields Summary
private static final ResourceBundle
_messages
I18N message handler for this class
private static final int
_defaultLevel
Default level if none supplied
private static final char
_levelSeparator
Level Separator for Logger Level specification
private static Map
_levelNamesToValues
private static boolean
_hasLoggingProperties
private static Properties
_loggingProperties
private final ResourceBundle
_bundle
Resource bundle for this (named) logger
private final String
_loggerName
Name for this logger
private final int
_level
Level for this logger
Constructors Summary
public AbstractLogger(String loggerName, String bundleName, ClassLoader loader)
Creates a new AbstractLogger. The supplied class loader or the loader which loaded this class must be able to load the bundle.

param
loggerName the full domain name of this logger
param
bundleName the bundle name for message translation
param
loader the loader used for looking up the bundle file and possibly the logging.properties or alternative file


	
	
		_levelNamesToValues = new HashMap();
		_levelNamesToValues.put("ALL", new Integer(ALL));		// NOI18N
		_levelNamesToValues.put("FINEST", new Integer(FINEST));	// NOI18N
		_levelNamesToValues.put("FINER", new Integer(FINER));	// NOI18N
		_levelNamesToValues.put("FINE", new Integer(FINE));		// NOI18N
		_levelNamesToValues.put("CONFIG", new Integer(CONFIG));	// NOI18N
		_levelNamesToValues.put("INFO", new Integer(INFO));		// NOI18N
		_levelNamesToValues.put("WARNING", new Integer(WARNING)); // NOI18N
		_levelNamesToValues.put("SEVERE", new Integer(SEVERE));	// NOI18N
		_levelNamesToValues.put("OFF", new Integer(OFF));		// NOI18N
	
		_loggerName = loggerName;
		_level = readLoggingLevel(loggerName);
		_bundle = I18NHelper.loadBundle(bundleName, loader);
	
Methods Summary
public voidconfig(java.lang.String msg)
Log a CONFIG message.

If the logger is currently enabled for the CONFIG message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)

 log(CONFIG, msg); 
public voidentering(java.lang.String sourceClass, java.lang.String sourceMethod)
Log a method entry.

This is a convenience method that can be used to log entry to a method. A LogRecord with message "ENTRY", log level FINER, and the given sourceMethod and sourceClass is logged.

param
sourceClass name of class that issued the logging request
param
sourceMethod name of method that is being entered

		entering(sourceClass, sourceMethod, (Object[])null);
	
public voidentering(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.Object param1)
Log a method entry, with one parameter.

This is a convenience method that can be used to log entry to a method. A LogRecord with message "ENTRY {0}", log level FINER, and the given sourceMethod, sourceClass, and parameter is logged.

param
sourceClass name of class that issued the logging request
param
sourceMethod name of method that is being entered
param
param1 parameter to the method being entered

		if (isLoggable(FINER))
			entering(sourceClass, sourceMethod, new Object[]{param1});
	
public voidentering(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.Object[] params)
Log a method entry, with an array of parameters.

This is a convenience method that can be used to log entry to a method. A LogRecord with message "ENTRY" (followed by a format {N} indicator for each entry in the parameter array), log level FINER, and the given sourceMethod, sourceClass, and parameters is logged.

param
sourceClass name of class that issued the logging request
param
sourceMethod name of method that is being entered
param
params array of parameters to the method being entered

		if (isLoggable(FINER))
		{
			MessageFormat messageFormat = null;
			String messageKey = null;
			String[] args = null;

			if ((params != null) && (params.length > 0))
			{
				messageKey = "entering_method_params";	// NOI18N
				args = new String[]{sourceClass, sourceMethod, 
					StringHelper.arrayToSeparatedList(Arrays.asList(params))};
			}
			else
			{
				messageKey = "entering_method";	// NOI18N
				args = new String[]{sourceClass, sourceMethod};
			}

			messageFormat = new MessageFormat(
				getMessages().getString(messageKey));
			finer(messageFormat.format(args));
		}
	
public voidexiting(java.lang.String sourceClass, java.lang.String sourceMethod)
Log a method return.

This is a convenience method that can be used to log returning from a method. A LogRecord with message "RETURN", log level FINER, and the given sourceMethod and sourceClass is logged.

param
sourceClass name of class that issued the logging request
param
sourceMethod name of the method

		exiting(sourceClass, sourceMethod, null);
	
public voidexiting(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.Object result)
Log a method return, with result object.

This is a convenience method that can be used to log returning from a method. A LogRecord with message "RETURN {0}", log level FINER, and the gives sourceMethod, sourceClass, and result object is logged.

param
sourceClass name of class that issued the logging request
param
sourceMethod name of the method
param
result Object that is being returned

		if (isLoggable(FINER))
		{
			MessageFormat messageFormat = null;
			String messageKey = null;
			Object[] args = null;

			if (result != null)
			{
				messageKey = "exiting_method_return";	// NOI18N
				args = new Object[]{sourceClass, sourceMethod, result};
			}
			else
			{
				messageKey = "exiting_method";	// NOI18N
				args = new Object[]{sourceClass, sourceMethod};
			}

			messageFormat = new MessageFormat(
				getMessages().getString(messageKey));
			finer(messageFormat.format(args));
		}
	
private java.lang.StringfindLevelMatch(java.lang.String loggerName)
Find the best matched logging level from the logging properties

param
loggerName the name of the hierarchical
return
a String representing the level value

		Properties properties = getLoggingProperties();
		String value = null;

		if (properties != null)
		{
			while (value == null)
			{
				int lastIndex = loggerName.lastIndexOf(_levelSeparator);

				value = properties.getProperty(loggerName + ".level"); // NOI18N

				if (loggerName.trim().length() > 0)
				{
					loggerName = ((lastIndex == -1) ? "" :		// NOI18N
						loggerName.substring(0, lastIndex));
				}
				else
					return value;
			}
		}

		return value;
	
public voidfine(java.lang.String msg)
Log a message.

If the logger is currently enabled for the message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)

 log(FINE, msg); 
public voidfine(java.lang.String msg, java.lang.Object o1)
Log a FINE message.

If the logger is currently enabled for the FINE message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)
param
o1 A parameter to be inserted into the message

 log(FINE, msg, o1); 
public voidfine(java.lang.String msg, java.lang.Object[] o)
Log a FINE message.

If the logger is currently enabled for the FINE message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)
param
o Objects to be inserted into the message

 log(FINE, msg, o); 
public voidfine(java.lang.String msg, java.lang.Object o1, java.lang.Object o2)
Log a FINE message.

If the logger is currently enabled for the FINE message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)
param
o1 A parameter to be inserted into the message
param
o2 A parameter to be inserted into the message

		log(FINE, msg, o1, o2);
	
public voidfine(java.lang.String msg, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3)
Log a FINE message.

If the logger is currently enabled for the FINE message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)
param
o1 A parameter to be inserted into the message
param
o2 A parameter to be inserted into the message
param
o3 A parameter to be inserted into the message

		log(FINE, msg, o1, o2, o3);
	
public voidfiner(java.lang.String msg)
Log a FINER message.

If the logger is currently enabled for the FINER message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)

 log(FINER, msg); 
public voidfiner(java.lang.String msg, java.lang.Object[] o)
Log a FINER message.

If the logger is currently enabled for the FINER message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)
param
o Objects to be inserted into the message

 log(FINER, msg, o); 
public voidfiner(java.lang.String msg, java.lang.Object o1)
Log a FINER message.

If the logger is currently enabled for the FINER message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)
param
o1 A parameter to be inserted into the message

 log(FINER, msg, o1); 
public voidfiner(java.lang.String msg, java.lang.Object o1, java.lang.Object o2)
Log a FINER message.

If the logger is currently enabled for the FINER message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)
param
o1 A parameter to be inserted into the message
param
o2 A parameter to be inserted into the message

		log(FINER, msg, o1, o2);
	
public voidfiner(java.lang.String msg, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3)
Log a FINER message.

If the logger is currently enabled for the FINER message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)
param
o1 A parameter to be inserted into the message
param
o2 A parameter to be inserted into the message
param
o3 A parameter to be inserted into the message

		log(FINER, msg, o1, o2, o3);
	
public voidfinest(java.lang.String msg)
Log a FINEST message.

If the logger is currently enabled for the FINEST message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)

 log(FINEST, msg); 
public voidfinest(java.lang.String msg, java.lang.Object[] o)
Log a FINEST message.

If the logger is currently enabled for the FINEST message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)
param
o Objects to be inserted into the message

 log(FINEST, msg, o); 
public voidfinest(java.lang.String msg, java.lang.Object o1)
Log a FINEST message.

If the logger is currently enabled for the FINEST message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)
param
o1 A parameter to be inserted into the message

 log(FINEST, msg, o1); 
public voidfinest(java.lang.String msg, java.lang.Object o1, java.lang.Object o2)
Log a FINEST message.

If the logger is currently enabled for the FINEST message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)
param
o1 A parameter to be inserted into the message
param
o2 A parameter to be inserted into the message

		log(FINEST, msg, o1, o2);
	
public voidfinest(java.lang.String msg, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3)
Log a FINEST message.

If the logger is currently enabled for the FINEST message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)
param
o1 A parameter to be inserted into the message
param
o2 A parameter to be inserted into the message
param
o3 A parameter to be inserted into the message

		log(FINEST, msg, o1, o2, o3);
	
protected java.util.ResourceBundlegetBundle()
Get the message bundle for the named instance of the logger.

 return _bundle; 
public intgetLevel()

 return _level; 
private static java.util.MapgetLevelNameMap()

 return _levelNamesToValues; 
private static synchronized java.util.PropertiesgetLoggingProperties()

		if (_hasLoggingProperties && (_loggingProperties == null))
		{
			String fileName = 
				System.getProperty("java.util.logging.config.file"); // NOI18N

			if (fileName == null)
			{
				fileName = System.getProperty("java.home");	// NOI18N

				if (fileName != null)
				{
					File file = new File(fileName, "lib");	// NOI18N

					file = new File(file, "logging.properties");	// NOI18N

					try
					{
						fileName = file.getCanonicalPath();
					}
					catch (IOException ioe)
					{
						fileName = null;
					}
				}
			}

			if (fileName != null)
			{
				InputStream inputStream = null;
				File file = null;

				try
				{
					Properties properties = new Properties();
					BufferedInputStream bufferedInputStream = null;

					inputStream = new FileInputStream(fileName);
					bufferedInputStream = new BufferedInputStream(inputStream);
					properties.load(bufferedInputStream);
					_loggingProperties = properties;
				}
				catch (Exception e)
				{
					_hasLoggingProperties = false;
				}
				finally
				{
					if (inputStream != null)
					{
						try
						{
							inputStream.close();
						}
						catch (IOException ioe)
						{
							// couldn't close it for some reason
						}
					}
				}
			}
			else
				_hasLoggingProperties = false;
		}

		return _loggingProperties;
	
protected java.lang.StringgetMessage(java.lang.String message)
This method returns a string from the bundle file if possible, treating the message argument as the key. If no such key is found in the bundle, the message itself is returned.

return
a message either used as itself or searched in the bundle file.
param
message the message which is used as a key to search the bundle

		try
		{
			return getBundle().getString(message);
		}
		catch (MissingResourceException e)
		{
			return message;
		}

	
protected java.lang.StringgetMessageWithPrefix(int level, java.lang.String message)
This method returns a string with a formatted prefix which depends on the level.

return
a formatted string with a level prefix.
param
level the level to print
param
message the message to print
see
AbstractLogger#toString

		MessageFormat messageFormat = new MessageFormat(
			getMessages().getString("logging_prefix"));	// NOI18N

		return messageFormat.format(
			new String[]{toString(level), message});
	
protected static java.util.ResourceBundlegetMessages()
Get the message bundle for the AbstractLogger itself.

 return _messages; 
public java.lang.StringgetName()
Get the name for this logger.

return
logger name. Will be null for anonymous Loggers.

 return _loggerName; 
public voidinfo(java.lang.String msg)
Log an INFO message.

If the logger is currently enabled for the INFO message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)

 log(INFO, msg); 
public booleanisLoggable()
Return whether logging is enabled at the FINE level. This method is not exact because to make it accurately reflect the logging level we would have to include the JDK 1.4 java.util.logging.Level class. This method does not delegate to isLoggable(FINE) for performance reasons.

return
whether logging is enabled at the fine level.

 return (FINE >= getLevel()); 
public booleanisLoggable(int levelValue)
Check if a message of the given level would actually be logged by this logger. This check is based on the Logger's effective level, which may be inherited from its parent.

return
true if the given message level is currently being logged.
param
levelValue the level to check

		return (levelValue >= getLevel());
	
public voidlog(int level, java.lang.String msg)
Log a message.

If the logger is currently enabled for the message level then the given message is forwarded to all the registered output Handler objects.

param
level The level for this message
param
msg The string message (or a key in the message catalog)

		if (isLoggable(level))
			logInternal(level, getMessage(msg));
	
public voidlog(int level, java.lang.String msg, java.lang.Object o1)
Log a message.

If the logger is currently enabled for the message level then the given message is forwarded to all the registered output Handler objects.

param
level The level for this message
param
msg The string message (or a key in the message catalog)
param
o1 A parameter to be inserted into the message

		if (isLoggable(level))
			log(level, msg, new Object[]{o1});
	
public voidlog(int level, java.lang.String msg, java.lang.Object[] o)
Log a message.

If the logger is currently enabled for the message level then the given message is forwarded to all the registered output Handler objects.

param
level The level for this message
param
msg The string message (or a key in the message catalog)
param
o Objects to be inserted into the message

		if (isLoggable(level))
		{
			int count = ((o == null) ? 0 : o.length);
			String formattedMessage = msg;

			if (count > 0)
			{
				MessageFormat messageFormat = 
					new MessageFormat(getBundle().getString(msg));

				if (messageFormat != null)
					formattedMessage = messageFormat.format(o);
			}
			else
				formattedMessage = getMessage(msg);

			logInternal(level, formattedMessage);
		}
	
public voidlog(int level, java.lang.String msg, java.lang.Object o1, java.lang.Object o2)
Log a message.

If the logger is currently enabled for the message level then the given message is forwarded to all the registered output Handler objects.

param
level The level for this message
param
msg The string message (or a key in the message catalog)
param
o1 A parameter to be inserted into the message
param
o2 A parameter to be inserted into the message

		if (isLoggable(level))
			log(level, msg, new Object[]{o1, o2});
	
public voidlog(int level, java.lang.String msg, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3)
Log a message.

If the logger is currently enabled for the message level then the given message is forwarded to all the registered output Handler objects.

param
level The level for this message
param
msg The string message (or a key in the message catalog)
param
o1 A parameter to be inserted into the message
param
o2 A parameter to be inserted into the message
param
o3 A parameter to be inserted into the message

		if (isLoggable(level))
			log(level, msg, new Object[]{o1, o2, o3});
	
public abstract voidlog(int level, java.lang.String msg, java.lang.Throwable thrown)
Log a message.

If the logger is currently enabled for the message level then the given message, and the exception dump, is forwarded to all the registered output Handler objects.

param
level The level for this message
param
msg The string message (or a key in the message catalog)
param
thrown The exception to log

protected abstract voidlogInternal(int level, java.lang.String message)
This method does the actual logging. It is expected that if a check for isLoggable is desired for performance reasons, it has already been done, as it should not be done here.

param
level the level to print
param
message the message to print

private intreadLoggingLevel(java.lang.String loggerName)

		String value =  findLevelMatch(loggerName);
		int level = _defaultLevel;

		if (value != null)
		{
			Object lookupValue = null;

			value = value.trim();
			lookupValue = getLevelNameMap().get(value);

			if (lookupValue != null)
				level = ((Integer)lookupValue).intValue();
			else		// maybe a number was specified
			{
				try
				{
					level = Integer.parseInt(value);
				}
				catch (NumberFormatException nfe)
				{
					// level will be the default
				}
			}
		}

		return level;
	
public voidsevere(java.lang.String msg)
Log a SEVERE message.

If the logger is currently enabled for the SEVERE message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)

 log(SEVERE, msg); 
public voidthrowing(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.Throwable thrown)
Log throwing an exception.

This is a convenience method to log that a method is terminating by throwing an exception. The logging is done using the FINER level.

If the logger is currently enabled for the given message level then the given arguments are stored in a LogRecord which is forwarded to all registered output handlers. The LogRecord's message is set to "THROW".

Note that the thrown argument is stored in the LogRecord thrown property, rather than the LogRecord parameters property. Thus is it processed specially by output Formatters and is not treated as a formatting parameter to the LogRecord message property.

param
sourceClass name of class that issued the logging request
param
sourceMethod name of the method.
param
thrown The Throwable that is being thrown.

		if (isLoggable(FINER))
		{
			MessageFormat messageFormat = new MessageFormat(
				getMessages().getString("throwing_method"));

			log(FINER, messageFormat.format(
				new String[]{sourceClass, sourceMethod}), thrown);
		}
	
public java.lang.StringtoString()
Prepare a printable version of this instance.

return
the String representation of this object

		StringBuffer buffer = new StringBuffer(getClass().getName());

		buffer.append(": ");				//NOI18N
		buffer.append(" name: ");			//NOI18N
		buffer.append(getName());
		buffer.append(", logging level: ");	//NOI18N
		buffer.append(toString(getLevel()));

		return buffer.toString();
	
public static java.lang.StringtoString(int level)
Return the string name of a level given its int value.

return
a string representing the level

		String bundleKey = null;

		switch (level)
		{
			case Logger.OFF:
				bundleKey = "utility.level_off";	// NOI18N
				break;
			case Logger.SEVERE:
				bundleKey = "utility.level_severe";	// NOI18N
				break;
			case Logger.WARNING:
				bundleKey = "utility.level_warning";	// NOI18N
				break;
			case Logger.INFO:
				bundleKey = "utility.level_info";	// NOI18N
				break;
			case Logger.CONFIG:
				bundleKey = "utility.level_config";	// NOI18N
				break;
			case Logger.FINE:
				bundleKey = "utility.level_fine";	// NOI18N
				break;
			case Logger.FINER:
				bundleKey = "utility.level_finer";	// NOI18N
				break;
			case Logger.FINEST:
				bundleKey = "utility.level_finest";	// NOI18N
				break;
			case Logger.ALL:
				bundleKey = "utility.level_all";	// NOI18N
				break;
		}

		return ((bundleKey != null) ? 
			I18NHelper.getMessage(getMessages(), bundleKey) : null);
	
public voidwarning(java.lang.String msg)
Log a WARNING message.

If the logger is currently enabled for the WARNING message level then the given message is forwarded to all the registered output Handler objects.

param
msg The string message (or a key in the message catalog)

 log(WARNING, msg);