Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at Symmetric Logger with internationalization.
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
symmetric/src/main/java/org/jumpmind/symmetric/common/logging/Log.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
symmetric/src/main/java/org/jumpmind/symmetric/common/logging/LogFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |