Index: jul-to-slf4j/src/test/java/org/slf4j/bridge/SLF4JBridgeHandlerTest.java =================================================================== --- jul-to-slf4j/src/test/java/org/slf4j/bridge/SLF4JBridgeHandlerTest.java (revision 1145) +++ jul-to-slf4j/src/test/java/org/slf4j/bridge/SLF4JBridgeHandlerTest.java (working copy) @@ -1,5 +1,7 @@ package org.slf4j.bridge; +import java.text.MessageFormat; +import java.util.ResourceBundle; import java.util.logging.Level; import org.apache.log4j.spi.LocationInfo; @@ -75,6 +77,56 @@ assertLevel(i++, org.apache.log4j.Level.ERROR); } + public void testLogWithResourceBundle(){ + SLF4JBridgeHandler.install(); + + String resourceBundleName = "org.slf4j.bridge.testLogStrings"; + ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName); + + String resourceKey1 = "resource_key_1"; + String expectedMsg1 = bundle.getString(resourceKey1); + + String resourceKey2 = "resource_key_2"; + Object [] params2 = new Object [] {"foo", "bar"}; + String expectedMsg2 = MessageFormat.format(bundle.getString(resourceKey2), params2); + + String resourceKey3 = "hello {0}"; + Object [] params3 = new Object [] {"John"}; + String expectedMsg3 = MessageFormat.format(resourceKey3, params3); + + java.util.logging.Logger julResourceBundleLogger = + java.util.logging.Logger.getLogger("foo",resourceBundleName); + + java.util.logging.Logger julLogger = + java.util.logging.Logger.getLogger("bar"); + + julResourceBundleLogger.info(resourceKey1); + julResourceBundleLogger.log(Level.INFO, resourceKey2, params2); + julResourceBundleLogger.log(Level.INFO, resourceKey3, params3); + + julLogger.log(Level.INFO, resourceKey3, params3); + + assertEquals(4, listAppender.list.size()); + + LoggingEvent le = null; + + le = (LoggingEvent) listAppender.list.get(0); + assertEquals("foo", le.getLoggerName()); + assertEquals(expectedMsg1, le.getMessage()); + + le = (LoggingEvent) listAppender.list.get(1); + assertEquals("foo", le.getLoggerName()); + assertEquals(expectedMsg2, le.getMessage()); + + le = (LoggingEvent) listAppender.list.get(2); + assertEquals("foo", le.getLoggerName()); + assertEquals(expectedMsg3, le.getMessage()); + + le = (LoggingEvent) listAppender.list.get(3); + assertEquals("bar", le.getLoggerName()); + assertEquals(expectedMsg3, le.getMessage()); + } + void assertLevel(int index, org.apache.log4j.Level expectedLevel) { LoggingEvent le = (LoggingEvent) listAppender.list.get(index); assertEquals(expectedLevel, le.getLevel()); Index: jul-to-slf4j/src/test/resources/org/slf4j/bridge/testLogStrings.properties =================================================================== --- jul-to-slf4j/src/test/resources/org/slf4j/bridge/testLogStrings.properties (revision 0) +++ jul-to-slf4j/src/test/resources/org/slf4j/bridge/testLogStrings.properties (revision 0) @@ -0,0 +1,4 @@ +resource_key_1=msg +resource_key_2=msg {0} {1} + + Property changes on: jul-to-slf4j/src/test/resources/org/slf4j/bridge/testLogStrings.properties ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Date Revision Added: svn:eol-style + native Index: jul-to-slf4j/src/main/java/org/slf4j/bridge/SLF4JBridgeHandler.java =================================================================== --- jul-to-slf4j/src/main/java/org/slf4j/bridge/SLF4JBridgeHandler.java (revision 1145) +++ jul-to-slf4j/src/main/java/org/slf4j/bridge/SLF4JBridgeHandler.java (working copy) @@ -31,6 +31,9 @@ package org.slf4j.bridge; +import java.text.MessageFormat; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogManager; @@ -158,21 +161,23 @@ } else { slf4jLevel = LocationAwareLogger.ERROR_INT; } - lal.log(null, FQCN, slf4jLevel, record.getMessage(), record.getThrown()); + String message = getMessage(record); + lal.log(null, FQCN, slf4jLevel, message, record.getThrown()); } protected void callPlainSLF4JLogger(Logger slf4jLogger, LogRecord record) { + String message = getMessage(record); int julLevelValue = record.getLevel().intValue(); if (julLevelValue <= TRACE_LEVEL_THRESHOLD) { - slf4jLogger.trace(record.getMessage(), record.getThrown()); + slf4jLogger.trace(message, record.getThrown()); } else if (julLevelValue <= DEBUG_LEVEL_THRESHOLD) { - slf4jLogger.debug(record.getMessage(), record.getThrown()); + slf4jLogger.debug(message, record.getThrown()); } else if (julLevelValue <= INFO_LEVEL_THRESHOLD) { - slf4jLogger.info(record.getMessage(), record.getThrown()); + slf4jLogger.info(message, record.getThrown()); } else if (julLevelValue <= WARN_LEVEL_THRESHOLD) { - slf4jLogger.warn(record.getMessage(), record.getThrown()); + slf4jLogger.warn(message, record.getThrown()); } else { - slf4jLogger.error(record.getMessage(), record.getThrown()); + slf4jLogger.error(message, record.getThrown()); } } @@ -206,4 +211,27 @@ callPlainSLF4JLogger(slf4jLogger, record); } } + + private String getMessage(LogRecord record){ + String message = record.getMessage(); + + if (message == null) { + return null; + } + + ResourceBundle bundle = record.getResourceBundle(); + if (bundle != null) { + try { + message = bundle.getString(message); + } catch (MissingResourceException e) { + } + } + + Object[] params = record.getParameters(); + if (params != null) { + message = MessageFormat.format(message, params); + } + + return message; + } }