Skip to content

Commit

Permalink
First pass at Symmetric Logger with internationalization.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanes committed Aug 15, 2009
1 parent bafd614 commit 5c6c7f9
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
106 changes: 106 additions & 0 deletions 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);
}

}
@@ -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));
}
}

0 comments on commit 5c6c7f9

Please sign in to comment.