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

ceki at slf4j.org ceki at slf4j.org
Fri Oct 3 20:54:25 CEST 2008


Author: ceki
Date: Fri Oct  3 20:54:25 2008
New Revision: 1177

Added:
   slf4j/trunk/jul-to-slf4j/src/test/resources/
   slf4j/trunk/jul-to-slf4j/src/test/resources/org/
   slf4j/trunk/jul-to-slf4j/src/test/resources/org/slf4j/
   slf4j/trunk/jul-to-slf4j/src/test/resources/org/slf4j/bridge/
   slf4j/trunk/jul-to-slf4j/src/test/resources/org/slf4j/bridge/testLogStrings.properties
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/SLF4JBridgeHandlerTest.java

Log:
Fixing bug 98 as reported by Darryl Smith

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 20:54:25 2008
@@ -31,6 +31,8 @@
 
 package org.slf4j.bridge;
 
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
 import java.util.logging.Handler;
 import java.util.logging.Level;
 import java.util.logging.LogManager;
@@ -40,40 +42,42 @@
 import org.slf4j.LoggerFactory;
 import org.slf4j.spi.LocationAwareLogger;
 
-
 // Based on http://bugzilla.slf4j.org/show_bug.cgi?id=38
 
 /**
  * Bridge/route all JUL log records to the SLF4J API.
  * 
- * <p>Essentially, the idea is to install on the root logger an instance of 
- * SLF4JBridgeHandler as the sole JUL handler in the system. Subsequently, the 
- * SLF4JBridgeHandler instance will redirect all JUL log records are redirected to 
- * the SLF4J API based on the following mapping of levels:
+ * <p>
+ * Essentially, the idea is to install on the root logger an instance of
+ * SLF4JBridgeHandler as the sole JUL handler in the system. Subsequently, the
+ * SLF4JBridgeHandler instance will redirect all JUL log records are redirected
+ * to the SLF4J API based on the following mapping of levels:
  * 
  * <pre>
- * FINEST  -> TRACE
- * FINER   -> DEBUG
- * FINE    -> DEBUG
- * INFO    -> INFO
- * WARNING -> WARN
- * SEVER   -> ERROR
+ * FINEST  -&gt; TRACE
+ * FINER   -&gt; DEBUG
+ * FINE    -&gt; DEBUG
+ * INFO    -&gt; INFO
+ * WARNING -&gt; WARN
+ * SEVER   -&gt; ERROR
  * </pre>
  * 
  * Usage:
  * 
  * <pre>
- *   // call only once during initialization time of your application
- *   SLF4JBridgeHandler.install();
- *   
- *   // usual pattern: get a Logger and then log a message
- *   java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger("org.wombat");
- *   julLogger.fine("hello world"); // this will get redirected to SLF4J
+ * // call only once during initialization time of your application
+ * SLF4JBridgeHandler.install();
+ * 
+ * // usual pattern: get a Logger and then log a message
+ * java.util.logging.Logger julLogger = java.util.logging.Logger
+ *     .getLogger(&quot;org.wombat&quot;);
+ * julLogger.fine(&quot;hello world&quot;); // this will get redirected to SLF4J
  * </pre>
  * 
  * @author Christian Stein
- * @author Joern Huxhorn        
+ * @author Joern Huxhorn
  * @author Ceki G&uml;lc&uml;
+ * @author Darryl Smith
  * 
  * @since 1.5.1
  */
@@ -94,7 +98,8 @@
    */
   public static void install() {
     LogManager.getLogManager().reset();
-    LogManager.getLogManager().getLogger("").addHandler(new SLF4JBridgeHandler());
+    LogManager.getLogManager().getLogger("").addHandler(
+        new SLF4JBridgeHandler());
   }
 
   /**
@@ -143,7 +148,8 @@
     return LoggerFactory.getLogger(name);
   }
 
-  protected void callLocationAwareLogger(LocationAwareLogger lal, LogRecord record) {
+  protected void callLocationAwareLogger(LocationAwareLogger lal,
+      LogRecord record) {
     int julLevelValue = record.getLevel().intValue();
     int slf4jLevel;
 
@@ -158,25 +164,50 @@
     } else {
       slf4jLevel = LocationAwareLogger.ERROR_INT;
     }
-    lal.log(null, FQCN, slf4jLevel, record.getMessage(), record.getThrown());
+    String i18nMessage = getMessageI18N(record);
+    lal.log(null, FQCN, slf4jLevel, i18nMessage, record.getThrown());
   }
 
   protected void callPlainSLF4JLogger(Logger slf4jLogger, LogRecord record) {
+    String i18nMessage = getMessageI18N(record);
     int julLevelValue = record.getLevel().intValue();
     if (julLevelValue <= TRACE_LEVEL_THRESHOLD) {
-      slf4jLogger.trace(record.getMessage(), record.getThrown());
+      slf4jLogger.trace(i18nMessage, record.getThrown());
     } else if (julLevelValue <= DEBUG_LEVEL_THRESHOLD) {
-      slf4jLogger.debug(record.getMessage(), record.getThrown());
+      slf4jLogger.debug(i18nMessage, record.getThrown());
     } else if (julLevelValue <= INFO_LEVEL_THRESHOLD) {
-      slf4jLogger.info(record.getMessage(), record.getThrown());
+      slf4jLogger.info(i18nMessage, record.getThrown());
     } else if (julLevelValue <= WARN_LEVEL_THRESHOLD) {
-      slf4jLogger.warn(record.getMessage(), record.getThrown());
+      slf4jLogger.warn(i18nMessage, record.getThrown());
     } else {
-      slf4jLogger.error(record.getMessage(), record.getThrown());
+      slf4jLogger.error(i18nMessage, record.getThrown());
     }
   }
 
   /**
+   * Get the record's message, possibly via a resource bundle.
+   * 
+   * @param record
+   * @return
+   */
+  private String getMessageI18N(LogRecord record) {
+    String rawMsg = record.getMessage();
+
+    if (rawMsg == null) {
+      return null;
+    }
+
+    ResourceBundle bundle = record.getResourceBundle();
+    if (bundle != null) {
+      try {
+        return bundle.getString(rawMsg);
+      } catch (MissingResourceException e) {
+      }
+    }
+    return rawMsg;
+  }
+  
+  /**
    * Publish a LogRecord.
    * <p>
    * The logging request was made initially to a Logger object, which
@@ -194,9 +225,11 @@
     if (record == null) {
       return;
     }
-    
+
     Logger slf4jLogger = getSLF4JLogger(record);
     String message = record.getMessage(); // can be null!
+    // this is a check to avoid calling the underlying logging system
+    // with a null message
     if (message == null) {
       return;
     }
@@ -206,4 +239,7 @@
       callPlainSLF4JLogger(slf4jLogger, record);
     }
   }
+
+
+
 }

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 20:54:25 2008
@@ -1,12 +1,12 @@
 package org.slf4j.bridge;
 
+import java.util.ResourceBundle;
 import java.util.logging.Level;
 
+import junit.framework.TestCase;
+
 import org.apache.log4j.spi.LocationInfo;
 import org.apache.log4j.spi.LoggingEvent;
-import org.slf4j.bridge.SLF4JBridgeHandler;
-
-import junit.framework.TestCase;
 
 public class SLF4JBridgeHandlerTest extends TestCase {
 
@@ -74,6 +74,26 @@
     assertLevel(i++, org.apache.log4j.Level.WARN);
     assertLevel(i++, org.apache.log4j.Level.ERROR);
   }
+  
+  public void testLogWithResourceBundle(){
+    SLF4JBridgeHandler.install();
+
+    String resourceBundleName = "org.slf4j.bridge.testLogStrings";
+    ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName);
+    String resourceKey = "resource_key";
+    String expectedMsg = bundle.getString(resourceKey);
+    String msg = resourceKey;
+
+    java.util.logging.Logger julResourceBundleLogger = java.util.logging.Logger
+        .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());
+
+  }
 
   void assertLevel(int index, org.apache.log4j.Level expectedLevel) {
     LoggingEvent le = (LoggingEvent) listAppender.list.get(index);

Added: slf4j/trunk/jul-to-slf4j/src/test/resources/org/slf4j/bridge/testLogStrings.properties
==============================================================================
--- (empty file)
+++ slf4j/trunk/jul-to-slf4j/src/test/resources/org/slf4j/bridge/testLogStrings.properties	Fri Oct  3 20:54:25 2008
@@ -0,0 +1 @@
+resource_key=msg



More information about the slf4j-dev mailing list