001: package org.objectweb.celtix.common.logging;
002:
003: import java.text.MessageFormat;
004: import java.util.ResourceBundle;
005: import java.util.logging.Level;
006: import java.util.logging.Logger;
007:
008: import org.objectweb.celtix.common.i18n.BundleUtils;
009:
010: /**
011: * A container for static utility methods related to logging.
012: */
013: public final class LogUtils {
014:
015: private static final Object[] NO_PARAMETERS = new Object[0];
016:
017: /**
018: * Prevents instantiation.
019: */
020: private LogUtils() {
021: }
022:
023: /**
024: * Get a Logger with an associated resource bundle.
025: *
026: * @param cls the Class to contain the Logger
027: * @return an appropriate Logger
028: */
029: public static Logger getL7dLogger(Class cls) {
030: return Logger.getLogger(cls.getName(), BundleUtils
031: .getBundleName(cls));
032: }
033:
034: /**
035: * Allows both parameter substitution and a typed Throwable to be logged.
036: *
037: * @param logger the Logger the log to
038: * @param level the severity level
039: * @param message the log message
040: * @param throwable the Throwable to log
041: * @param parameter the parameter to substitute into message
042: */
043: public static void log(Logger logger, Level level, String message,
044: Throwable throwable, Object parameter) {
045: if (logger.isLoggable(level)) {
046: final String formattedMessage = MessageFormat.format(
047: localize(logger, message), parameter);
048: logger.log(level, formattedMessage, throwable);
049: }
050: }
051:
052: /**
053: * Allows both parameter substitution and a typed Throwable to be logged.
054: *
055: * @param logger the Logger the log to
056: * @param level the severity level
057: * @param message the log message
058: * @param throwable the Throwable to log
059: * @param parameters the parameters to substitute into message
060: */
061: public static void log(Logger logger, Level level, String message,
062: Throwable throwable, Object... parameters) {
063: if (logger.isLoggable(level)) {
064: final String formattedMessage = MessageFormat.format(
065: localize(logger, message), parameters);
066: logger.log(level, formattedMessage, throwable);
067: }
068: }
069:
070: /**
071: * Checks log level and logs
072: *
073: * @param logger the Logger the log to
074: * @param level the severity level
075: * @param message the log message
076: */
077: public static void log(Logger logger, Level level, String message) {
078: if (logger.isLoggable(level)) {
079: final String formattedMessage = MessageFormat.format(
080: localize(logger, message), NO_PARAMETERS);
081: logger.log(level, formattedMessage);
082: }
083:
084: }
085:
086: /**
087: * Checks log level and logs
088: *
089: * @param logger the Logger the log to
090: * @param level the severity level
091: * @param message the log message
092: * @param parameters the parameters to substitute into message
093: */
094: public static void log(Logger logger, Level level, String message,
095: Object[] parameters) {
096: if (logger.isLoggable(level)) {
097: final String formattedMessage = MessageFormat.format(
098: localize(logger, message), parameters);
099: logger.log(level, formattedMessage);
100: }
101:
102: }
103:
104: /**
105: * Retreive localized message retreived from a logger's resource
106: * bundle.
107: *
108: * @param logger the Logger
109: * @param message the message to be localized
110: */
111: private static String localize(Logger logger, String message) {
112: ResourceBundle bundle = logger.getResourceBundle();
113: return bundle != null ? bundle.getString(message) : message;
114: }
115:
116: }
|