diff --git a/symmetric/src/main/java/org/jumpmind/symmetric/common/logging/Log.java b/symmetric/src/main/java/org/jumpmind/symmetric/common/logging/Log.java new file mode 100644 index 0000000000..f2b9c420b3 --- /dev/null +++ b/symmetric/src/main/java/org/jumpmind/symmetric/common/logging/Log.java @@ -0,0 +1,106 @@ +package org.jumpmind.symmetric.common.logging; + +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; +import java.util.UnknownFormatConversionException; + +public class Log { + + private org.apache.commons.logging.Log logger = null; + + private Locale locale = null; + + private String bundleName = null; + + private ResourceBundle bundle = null; + private static String MESSAGE_KEY = "MessageKey: "; + private static String DEFAULT_BUNDLE_NAME = "logMessages"; + + Log(org.apache.commons.logging.Log logger) { + this(logger, DEFAULT_BUNDLE_NAME, Locale.getDefault()); + } + + Log(org.apache.commons.logging.Log logger, String bundleName) { + this(logger, bundleName, Locale.getDefault()); + } + + Log(org.apache.commons.logging.Log logger, String bundleName, Locale locale) { + this.logger = logger; + this.bundleName = bundleName; + setLocale(locale); + } + + public String getMessage(String key) { + return getMessage(key, (Object) null); + } + + public String getMessage(String key, Object... args) { + if (bundle != null) { + try { + if (args != null) { + return String.format(locale, bundle.getString(key), args); + } else { + return String.format(locale, bundle.getString(key)); + } + } catch (MissingResourceException mre) { + return MESSAGE_KEY + key + ((args != null) ? args.toString() : ""); + } catch (UnknownFormatConversionException ufce) { + logger.error(MESSAGE_KEY + key + " has a bad format type: " + bundle.getString(key)); + return MESSAGE_KEY + key + ((args != null) ? args.toString() : ""); + } + } else { + return MESSAGE_KEY + key + ((args != null) ? args.toString() : ""); + } + } + + // Debug + + public void debug(String messageKey) { + if (logger.isDebugEnabled()) { + logger.debug(getMessage(messageKey)); + } + } + + public void debug(String messageKey, Throwable t) { + if (logger.isDebugEnabled()) { + logger.debug(getMessage(messageKey), t); + } + } + + public void debug(String messageKey, Object... args) { + if (logger.isDebugEnabled()) { + logger.debug(getMessage(messageKey, args)); + } + } + + public void debug(String messageKey, Throwable t, Object... args) { + if (logger.isDebugEnabled()) { + logger.debug(getMessage(messageKey, args), t); + } + } + + public Locale getLocale() { + return locale; + } + + public void setLocale(Locale locale) { + this.locale = locale; + try { + this.bundle = ResourceBundle.getBundle(bundleName, locale); + } catch (MissingResourceException e) { + this.bundle = null; + logger.error("Unable to locate resource bundle: " + bundleName); + } + } + + public String getBundleName() { + return bundleName; + } + + public void setBundleName(String bundleName) { + this.bundleName = bundleName; + this.bundle = ResourceBundle.getBundle(bundleName, locale); + } + +} diff --git a/symmetric/src/main/java/org/jumpmind/symmetric/common/logging/LogFactory.java b/symmetric/src/main/java/org/jumpmind/symmetric/common/logging/LogFactory.java new file mode 100644 index 0000000000..72cf18c995 --- /dev/null +++ b/symmetric/src/main/java/org/jumpmind/symmetric/common/logging/LogFactory.java @@ -0,0 +1,11 @@ +package org.jumpmind.symmetric.common.logging; + +import org.apache.commons.logging.LogConfigurationException; + +public class LogFactory { + + @SuppressWarnings("unchecked") + public static Log getLog(Class clazz) throws LogConfigurationException { + return new Log(org.apache.commons.logging.LogFactory.getLog(clazz)); + } +}