[slf4j-dev] svn commit: r1178 - in slf4j/trunk/jul-to-slf4j/src: main/java/org/slf4j/bridge test/java/org/slf4j/bridge test/resources/org/slf4j/bridge

ceki at slf4j.org ceki at slf4j.org
Fri Oct 3 21:09:34 CEST 2008


Author: ceki
Date: Fri Oct  3 21:09:33 2008
New Revision: 1178

Modified:
   slf4j/trunk/jul-to-slf4j/src/main/java/org/slf4j/bridge/SLF4JBridgeHandler.java
   slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/ListAppender.java
   slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/SLF4JBridgeHandlerTest.java
   slf4j/trunk/jul-to-slf4j/src/test/resources/org/slf4j/bridge/testLogStrings.properties

Log:
- added parameter support as requested in bug 103 by Jarek Gawor

Modified: slf4j/trunk/jul-to-slf4j/src/main/java/org/slf4j/bridge/SLF4JBridgeHandler.java
==============================================================================
--- slf4j/trunk/jul-to-slf4j/src/main/java/org/slf4j/bridge/SLF4JBridgeHandler.java	(original)
+++ slf4j/trunk/jul-to-slf4j/src/main/java/org/slf4j/bridge/SLF4JBridgeHandler.java	Fri Oct  3 21:09:33 2008
@@ -31,6 +31,7 @@
 
 package org.slf4j.bridge;
 
+import java.text.MessageFormat;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 import java.util.logging.Handler;
@@ -191,22 +192,26 @@
    * @return
    */
   private String getMessageI18N(LogRecord record) {
-    String rawMsg = record.getMessage();
+    String message = record.getMessage();
 
-    if (rawMsg == null) {
+    if (message == null) {
       return null;
     }
 
     ResourceBundle bundle = record.getResourceBundle();
     if (bundle != null) {
       try {
-        return bundle.getString(rawMsg);
+        message = bundle.getString(message);
       } catch (MissingResourceException e) {
       }
     }
-    return rawMsg;
+    Object[] params = record.getParameters();
+    if (params != null) {
+      message = MessageFormat.format(message, params);
+    }
+    return message;
   }
-  
+
   /**
    * Publish a LogRecord.
    * <p>
@@ -240,6 +245,4 @@
     }
   }
 
-
-
 }

Modified: slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/ListAppender.java
==============================================================================
--- slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/ListAppender.java	(original)
+++ slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/ListAppender.java	Fri Oct  3 21:09:33 2008
@@ -1,3 +1,27 @@
+/*
+ * Copyright (c) 2004-2008 QOS.ch
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
 package org.slf4j.bridge;
 
 import java.util.ArrayList;

Modified: slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/SLF4JBridgeHandlerTest.java
==============================================================================
--- slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/SLF4JBridgeHandlerTest.java	(original)
+++ slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/SLF4JBridgeHandlerTest.java	Fri Oct  3 21:09:33 2008
@@ -1,5 +1,30 @@
+/*
+ * Copyright (c) 2004-2008 QOS.ch
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
 package org.slf4j.bridge;
 
+import java.text.MessageFormat;
 import java.util.ResourceBundle;
 import java.util.logging.Level;
 
@@ -74,8 +99,8 @@
     assertLevel(i++, org.apache.log4j.Level.WARN);
     assertLevel(i++, org.apache.log4j.Level.ERROR);
   }
-  
-  public void testLogWithResourceBundle(){
+
+  public void testLogWithResourceBundle() {
     SLF4JBridgeHandler.install();
 
     String resourceBundleName = "org.slf4j.bridge.testLogStrings";
@@ -85,14 +110,61 @@
     String msg = resourceKey;
 
     java.util.logging.Logger julResourceBundleLogger = java.util.logging.Logger
-        .getLogger("yay",resourceBundleName);
+        .getLogger("yay", resourceBundleName);
 
     julResourceBundleLogger.info(msg);
     assertEquals(1, listAppender.list.size());
     LoggingEvent le = (LoggingEvent) listAppender.list.get(0);
     assertEquals(LOGGER_NAME, le.getLoggerName());
     assertEquals(expectedMsg, le.getMessage());
+  }
+
+  public void testLogWithResourceBundleWithParameters() {
+    SLF4JBridgeHandler.install();
+
+    String resourceBundleName = "org.slf4j.bridge.testLogStrings";
+    ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName);
+
+    java.util.logging.Logger julResourceBundleLogger = java.util.logging.Logger
+        .getLogger("foo", resourceBundleName);
+
+    String resourceKey1 = "resource_key_1";
+    String expectedMsg1 = bundle.getString(resourceKey1);
+    julResourceBundleLogger.info(resourceKey1); // 1st log
+
+    String resourceKey2 = "resource_key_2";
+    Object[] params2 = new Object[] { "foo", "bar" };
+    String expectedMsg2 = MessageFormat.format(bundle.getString(resourceKey2),
+        params2);
+    julResourceBundleLogger.log(Level.INFO, resourceKey2, params2); // 2nd log
+
+    
+    String resourceKey3 = "invalidKey {0}";
+    Object[] params3 = new Object[] { "John" };
+    String expectedMsg3 = MessageFormat.format(resourceKey3, params3);
+    julResourceBundleLogger.log(Level.INFO, resourceKey3, params3); // 3rd log
+
+    julLogger.log(Level.INFO, resourceKey3, params3);  // 4th log
+
+    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("yay", le.getLoggerName());
+    assertEquals(expectedMsg3, le.getMessage());
   }
 
   void assertLevel(int index, org.apache.log4j.Level expectedLevel) {

Modified: slf4j/trunk/jul-to-slf4j/src/test/resources/org/slf4j/bridge/testLogStrings.properties
==============================================================================
--- slf4j/trunk/jul-to-slf4j/src/test/resources/org/slf4j/bridge/testLogStrings.properties	(original)
+++ slf4j/trunk/jul-to-slf4j/src/test/resources/org/slf4j/bridge/testLogStrings.properties	Fri Oct  3 21:09:33 2008
@@ -1 +1,3 @@
 resource_key=msg
+resource_key_1=msg
+resource_key_2=msg {0} {1}



More information about the slf4j-dev mailing list