[slf4j-dev] svn commit: r722 - in slf4j/trunk: jcl104-over-slf4j/src/main/java/org/apache/commons/logging/impl slf4j-api/src/main/java/org/slf4j/spi slf4j-jdk14/src/main/java/org/slf4j/impl slf4j-log4j12/src/main/java/org/slf4j/impl

ceki at slf4j.org ceki at slf4j.org
Fri Feb 9 22:49:31 CET 2007


Author: ceki
Date: Fri Feb  9 22:49:30 2007
New Revision: 722

Added:
   slf4j/trunk/jcl104-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4JLocationAwareLog.java
   slf4j/trunk/slf4j-api/src/main/java/org/slf4j/spi/LocationAwareLogger.java
Modified:
   slf4j/trunk/jcl104-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4FLogFactory.java
   slf4j/trunk/slf4j-jdk14/src/main/java/org/slf4j/impl/JDK14LoggerAdapter.java
   slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jLoggerAdapter.java

Log:

It looks like theres is a simple fix for bug#23 (see [1]). It won't impact existing clients, not existing bindings. 

[1] http://bugzilla.slf4j.org/show_bug.cgi?id=23



Modified: slf4j/trunk/jcl104-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4FLogFactory.java
==============================================================================
--- slf4j/trunk/jcl104-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4FLogFactory.java	(original)
+++ slf4j/trunk/jcl104-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4FLogFactory.java	Fri Feb  9 22:49:30 2007
@@ -27,6 +27,7 @@
 import org.apache.commons.logging.LogFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.slf4j.spi.LocationAwareLogger;
 
 /**
  * <p>
@@ -152,7 +153,11 @@
       instance = (Log) loggerMap.get(name);
       if (instance == null) {
         Logger logger = LoggerFactory.getLogger(name);
-        instance = new SLF4JLog(logger);
+        if(logger instanceof LocationAwareLogger) {
+          instance = new SLF4JLocationAwareLog((LocationAwareLogger) logger);
+        } else {
+          instance = new SLF4JLog(logger);
+        }
         loggerMap.put(name, instance);
       }
     }

Added: slf4j/trunk/jcl104-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4JLocationAwareLog.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/jcl104-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4JLocationAwareLog.java	Fri Feb  9 22:49:30 2007
@@ -0,0 +1,200 @@
+// TOTO
+ 
+package org.apache.commons.logging.impl;
+
+import org.apache.commons.logging.Log;
+import org.slf4j.Logger;
+import org.slf4j.spi.LocationAwareLogger;
+
+/**
+ * Implementation of {@link Log org.apache.commons.logging.Log} interface which 
+ * delegates all processing to a wrapped {@link Logger org.slf4j.Logger} instance.
+ * 
+ * <p>JCL's FATAL and TRACE levels are mapped to ERROR and DEBUG respectively. All 
+ * other levels map one to one.
+ * 
+ * @author Ceki G&uuml;lc&uuml;
+ */
+public class SLF4JLocationAwareLog implements Log {
+
+  private LocationAwareLogger logger;
+  private static final String FQCN = SLF4JLocationAwareLog.class.getName();
+  
+  SLF4JLocationAwareLog(LocationAwareLogger logger) {
+    this.logger = logger;
+  }
+
+  /**
+   * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
+   */
+  public boolean isDebugEnabled() {
+    return logger.isDebugEnabled();
+  }
+
+  /**
+   * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
+   */
+  public boolean isErrorEnabled() {
+    return logger.isErrorEnabled();
+  }
+
+  /**
+   * Delegates to the <code>isErrorEnabled<code> method of the wrapped 
+   * <code>org.slf4j.Logger</code> instance.
+   */
+  public boolean isFatalEnabled() {
+    return logger.isErrorEnabled();
+  }
+
+  /**
+   * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
+   */
+  public boolean isInfoEnabled() {
+    return logger.isInfoEnabled();
+  }
+
+  /**
+   * Delegates to the <code>isDebugEnabled<code> method of the wrapped 
+   * <code>org.slf4j.Logger</code> instance.
+   */
+  public boolean isTraceEnabled() {
+    return logger.isDebugEnabled();
+  }
+
+  /**
+   * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
+   */
+  public boolean isWarnEnabled() {
+    return logger.isWarnEnabled();
+  }
+
+  /**
+   * Converts the input parameter to String and then delegates to 
+   * the debug method of the wrapped <code>org.slf4j.Logger</code> instance.
+   * 
+   * @param message the message to log. Converted to {@link String}  
+   */
+  public void trace(Object message) {
+    logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), null);
+  }
+
+  /**
+   * Converts the first input parameter to String and then delegates to 
+   * the debug method of the wrapped <code>org.slf4j.Logger</code> instance.
+   * 
+   * @param message the message to log. Converted to {@link String}  
+   * @param t the exception to log
+   */
+  public void trace(Object message, Throwable t) {
+    logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), t);
+  }
+
+  /**
+   * Converts the input parameter to String and then delegates to the wrapped 
+   * <code>org.slf4j.Logger</code> instance.
+   * 
+   * @param message the message to log. Converted to {@link String} 
+   */
+  public void debug(Object message) {
+    logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), null);
+  }
+
+  /**
+   * Converts the first input parameter to String and then delegates to 
+   * the wrapped <code>org.slf4j.Logger</code> instance.
+   * 
+   * @param message the message to log. Converted to {@link String}  
+   * @param t the exception to log
+   */
+  public void debug(Object message, Throwable t) {
+    logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), t);
+  }
+
+  /**
+   * Converts the input parameter to String and then delegates to the wrapped 
+   * <code>org.slf4j.Logger</code> instance.
+   * 
+   * @param message the message to log. Converted to {@link String} 
+   */
+  public void info(Object message) {
+    logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), null);
+  }
+
+  /**
+   * Converts the first input parameter to String and then delegates to 
+   * the wrapped <code>org.slf4j.Logger</code> instance.
+   * 
+   * @param message the message to log. Converted to {@link String}  
+   * @param t the exception to log
+   */
+  public void info(Object message, Throwable t) {
+    logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), t);
+  }
+
+  /**
+   * Converts the input parameter to String and then delegates to the wrapped 
+   * <code>org.slf4j.Logger</code> instance.
+   * 
+   * @param message the message to log. Converted to {@link String}  
+   */
+  public void warn(Object message) {
+    logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), null);
+  }
+
+  /**
+   * Converts the first input parameter to String and then delegates to 
+   * the wrapped <code>org.slf4j.Logger</code> instance.
+   * 
+   * @param message the message to log. Converted to {@link String}  
+   * @param t the exception to log
+   */
+  public void warn(Object message, Throwable t) {
+    logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), t);
+  }
+
+  /**
+   * Converts the input parameter to String and then delegates to the wrapped 
+   * <code>org.slf4j.Logger</code> instance.
+   * 
+   * @param message the message to log. Converted to {@link String}  
+   */
+  public void error(Object message) {
+    logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null);
+  }
+
+  /**
+   * Converts the first input parameter to String and then delegates to 
+   * the wrapped <code>org.slf4j.Logger</code> instance.
+   * 
+   * @param message the message to log. Converted to {@link String}  
+   * @param t the exception to log
+   */
+  public void error(Object message, Throwable t) {
+    logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), t);
+    logger.error(String.valueOf(message), t);
+  }
+
+
+ 
+  /**
+   * Converts the input parameter to String and then delegates to 
+   * the error method of the wrapped <code>org.slf4j.Logger</code> instance.
+   * 
+   * @param message the message to log. Converted to {@link String}  
+   */
+  public void fatal(Object message) {
+    logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null);
+  }
+
+  /**
+   * Converts the first input parameter to String and then delegates to 
+   * the error method of the wrapped <code>org.slf4j.Logger</code> instance.
+   * 
+   * @param message the message to log. Converted to {@link String}  
+   * @param t the exception to log
+   */
+  public void fatal(Object message, Throwable t) {
+    logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), t);
+  }
+
+}
\ No newline at end of file

Added: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/spi/LocationAwareLogger.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/spi/LocationAwareLogger.java	Fri Feb  9 22:49:30 2007
@@ -0,0 +1,35 @@
+package org.slf4j.spi;
+
+import org.slf4j.Logger;
+import org.slf4j.Marker;
+
+/**
+ * An <b>optional</b> interface helping integration with logging systems capable of 
+ * extracting location information. This interface is mainly used by SLF4J bridges 
+ * such as jcl104-over-slf4j which need to provide hints so that the underlying logging
+ * system can extract the correct locatin information (method name, line number, etc.).
+ * 
+ * 
+ * @author Ceki Gulcu
+ * @since 1.3
+ */
+public interface LocationAwareLogger extends Logger {
+
+  final public int DEBUG_INT = 0;
+  final public int INFO_INT = 1;
+  final public int WARN_INT = 2;
+  final public int ERROR_INT = 3;
+  
+  
+  /**
+   * Printing method which support for location information. 
+   * 
+   * @param marker
+   * @param fqcn The fully qualified class name of the <b>caller</b>
+   * @param level
+   * @param message
+   * @param t
+   */  
+  public void log(Marker marker, String fqcn, int level, String message, Throwable t);
+  
+}

Modified: slf4j/trunk/slf4j-jdk14/src/main/java/org/slf4j/impl/JDK14LoggerAdapter.java
==============================================================================
--- slf4j/trunk/slf4j-jdk14/src/main/java/org/slf4j/impl/JDK14LoggerAdapter.java	(original)
+++ slf4j/trunk/slf4j-jdk14/src/main/java/org/slf4j/impl/JDK14LoggerAdapter.java	Fri Feb  9 22:49:30 2007
@@ -37,6 +37,8 @@
 import java.util.logging.LogRecord;
 
 import org.slf4j.Logger;
+import org.slf4j.Marker;
+import org.slf4j.spi.LocationAwareLogger;
 
 /**
  * A wrapper over {@link java.util.logging.Logger java.util.logging.Logger} in
@@ -47,7 +49,7 @@
  * @author Ceki G&uuml;lc&uuml;
  * @author Peter Royal
  */
-public final class JDK14LoggerAdapter extends MarkerIgnoringBase {
+public final class JDK14LoggerAdapter extends MarkerIgnoringBase implements LocationAwareLogger {
   final java.util.logging.Logger logger;
 
   // WARN: JDK14LoggerAdapter constructor should have only package access so
@@ -78,7 +80,7 @@
    *          the message object to be logged
    */
   public void debug(String msg) {
-    log(Level.FINE, msg, null);
+    log(SELF, Level.FINE, msg, null);
   }
 
   /**
@@ -97,7 +99,7 @@
   public void debug(String format, Object arg) {
     if (logger.isLoggable(Level.FINE)) {
       String msgStr = MessageFormatter.format(format, arg);
-      log(Level.FINE, msgStr, null);
+      log(SELF, Level.FINE, msgStr, null);
     }
   }
 
@@ -120,7 +122,7 @@
   public void debug(String format, Object arg1, Object arg2) {
     if (logger.isLoggable(Level.FINE)) {
       String msgStr = MessageFormatter.format(format, arg1, arg2);
-      log(Level.FINE, msgStr, null);
+      log(SELF, Level.FINE, msgStr, null);
     }
   }
 
@@ -141,7 +143,7 @@
   public void debug(String format, Object[] argArray) {
     if (logger.isLoggable(Level.FINE)) {
       String msgStr = MessageFormatter.arrayFormat(format, argArray);
-      log(Level.FINE, msgStr, null);
+      log(SELF, Level.FINE, msgStr, null);
     }
   }
 
@@ -154,7 +156,7 @@
    *          the exception (throwable) to log
    */
   public void debug(String msg, Throwable t) {
-    log(Level.FINE, msg, t);
+    log(SELF, Level.FINE, msg, t);
   }
 
   /**
@@ -173,7 +175,7 @@
    *          the message object to be logged
    */
   public void info(String msg) {
-    log(Level.INFO, msg, null);
+    log(SELF, Level.INFO, msg, null);
   }
 
   /**
@@ -192,7 +194,7 @@
   public void info(String format, Object arg) {
     if (logger.isLoggable(Level.INFO)) {
       String msgStr = MessageFormatter.format(format, arg);
-      log(Level.INFO, msgStr, null);
+      log(SELF, Level.INFO, msgStr, null);
     }
   }
 
@@ -215,7 +217,7 @@
   public void info(String format, Object arg1, Object arg2) {
     if (logger.isLoggable(Level.INFO)) {
       String msgStr = MessageFormatter.format(format, arg1, arg2);
-      log(Level.INFO, msgStr, null);
+      log(SELF, Level.INFO, msgStr, null);
     }
   }
 
@@ -236,7 +238,7 @@
   public void info(String format, Object[] argArray) {
     if (logger.isLoggable(Level.INFO)) {
       String msgStr = MessageFormatter.arrayFormat(format, argArray);
-      log(Level.INFO, msgStr, null);
+      log(SELF, Level.INFO, msgStr, null);
     }
   }
 
@@ -250,7 +252,7 @@
    *          the exception (throwable) to log
    */
   public void info(String msg, Throwable t) {
-    log(Level.INFO, msg, t);
+    log(SELF, Level.INFO, msg, t);
   }
 
   /**
@@ -270,7 +272,7 @@
    *          the message object to be logged
    */
   public void warn(String msg) {
-    log(Level.WARNING, msg, null);
+    log(SELF, Level.WARNING, msg, null);
   }
 
   /**
@@ -290,7 +292,7 @@
   public void warn(String format, Object arg) {
     if (logger.isLoggable(Level.WARNING)) {
       String msgStr = MessageFormatter.format(format, arg);
-      log(Level.WARNING, msgStr, null);
+      log(SELF, Level.WARNING, msgStr, null);
     }
   }
 
@@ -313,7 +315,7 @@
   public void warn(String format, Object arg1, Object arg2) {
     if (logger.isLoggable(Level.WARNING)) {
       String msgStr = MessageFormatter.format(format, arg1, arg2);
-      log(Level.WARNING, msgStr, null);
+      log(SELF, Level.WARNING, msgStr, null);
     }
   }
 
@@ -334,7 +336,7 @@
   public void warn(String format, Object[] argArray) {
     if (logger.isLoggable(Level.WARNING)) {
       String msgStr = MessageFormatter.arrayFormat(format, argArray);
-      log(Level.WARNING, msgStr, null);
+      log(SELF, Level.WARNING, msgStr, null);
     }
   }
 
@@ -348,7 +350,7 @@
    *          the exception (throwable) to log
    */
   public void warn(String msg, Throwable t) {
-    log(Level.WARNING, msg, t);
+    log(SELF, Level.WARNING, msg, t);
   }
 
   /**
@@ -367,7 +369,7 @@
    *          the message object to be logged
    */
   public void error(String msg) {
-    log(Level.SEVERE, msg, null);
+    log(SELF, Level.SEVERE, msg, null);
   }
 
   /**
@@ -387,7 +389,7 @@
   public void error(String format, Object arg) {
     if (logger.isLoggable(Level.SEVERE)) {
       String msgStr = MessageFormatter.format(format, arg);
-      log(Level.SEVERE, msgStr, null);
+      log(SELF, Level.SEVERE, msgStr, null);
     }
   }
 
@@ -410,7 +412,7 @@
   public void error(String format, Object arg1, Object arg2) {
     if (logger.isLoggable(Level.SEVERE)) {
       String msgStr = MessageFormatter.format(format, arg1, arg2);
-      log(Level.SEVERE, msgStr, null);
+      log(SELF, Level.SEVERE, msgStr, null);
     }
   }
 
@@ -431,7 +433,7 @@
   public void error(String format, Object[] argArray) {
     if (logger.isLoggable(Level.SEVERE)) {
       String msgStr = MessageFormatter.arrayFormat(format, argArray);
-      log(Level.SEVERE, msgStr, null);
+      log(SELF, Level.SEVERE, msgStr, null);
     }
   }
 
@@ -445,7 +447,7 @@
    *          the exception (throwable) to log
    */
   public void error(String msg, Throwable t) {
-    log(Level.SEVERE, msg, t);
+    log(SELF, Level.SEVERE, msg, t);
   }
 
   
@@ -459,31 +461,30 @@
    * @param msg
    * @param t
    */
-  private void log(Level level, String msg, Throwable t) {
+  private void log(String callerFQCN, Level level, String msg, Throwable t) {
     // millis and thread are filled by the constructor
     LogRecord record = new LogRecord(level, msg);
     record.setLoggerName(getName());
     record.setThrown(t);
-    fillCallerData(record);
+    fillCallerData(callerFQCN, record);
     logger.log(record);
 
   }
 
   static String SELF = JDK14LoggerAdapter.class.getName();
   static String SUPER = MarkerIgnoringBase.class.getName();
-
   /**
    * Fill in caller data if possible. 
    * 
    * @param record The record to update
    */
-  final private void fillCallerData(LogRecord record) {
+  final private void fillCallerData(String callerFQCN, LogRecord record) {
     StackTraceElement[] steArray = new Throwable().getStackTrace();
 
     int selfIndex = -1;
     for (int i = 0; i < steArray.length; i++) {
       final String className = steArray[i].getClassName();
-      if (className.equals(SELF) || className.equals(SUPER)) {
+      if (className.equals(callerFQCN) || className.equals(SUPER)) {
         selfIndex = i;
         break;
       }
@@ -492,7 +493,7 @@
     int found = -1;
     for (int i = selfIndex + 1; i < steArray.length; i++) {
       final String className = steArray[i].getClassName();
-      if (!(className.equals(SELF) || className.equals(SUPER))) {
+      if (!(className.equals(callerFQCN) || className.equals(SUPER))) {
         found = i;
         break;
       }
@@ -506,4 +507,25 @@
       record.setSourceMethodName(ste.getMethodName());
     }
   }
+
+  public void log(Marker marker, String callerFQCN, int level, String message, Throwable t) {
+    Level julLevel;
+    switch(level) {
+      case LocationAwareLogger.DEBUG_INT: 
+        julLevel = Level.FINE;
+        break;
+      case LocationAwareLogger.INFO_INT: 
+        julLevel = Level.INFO;
+        break;
+      case LocationAwareLogger.WARN_INT: 
+        julLevel = Level.WARNING;
+        break;
+      case LocationAwareLogger.ERROR_INT: 
+        julLevel = Level.SEVERE;
+        break;
+      default:
+        throw new IllegalStateException("Level number "+level+" is not recognized.");
+    }
+    log(callerFQCN, julLevel, message, t);    
+  }
 }

Modified: slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jLoggerAdapter.java
==============================================================================
--- slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jLoggerAdapter.java	(original)
+++ slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jLoggerAdapter.java	Fri Feb  9 22:49:30 2007
@@ -36,6 +36,8 @@
 
 import org.apache.log4j.Level;
 import org.slf4j.Logger;
+import org.slf4j.Marker;
+import org.slf4j.spi.LocationAwareLogger;
 
 
 /**
@@ -48,7 +50,7 @@
 
  * @author Ceki G&uuml;lc&uuml;
  */
-public final class Log4jLoggerAdapter extends MarkerIgnoringBase {
+public final class Log4jLoggerAdapter extends MarkerIgnoringBase implements LocationAwareLogger {
   final org.apache.log4j.Logger logger;
   
   /**
@@ -397,4 +399,25 @@
   public void error(String msg, Throwable t) {
     logger.log(FQCN, Level.ERROR, msg, t);
   }
+
+  public void log(Marker marker, String callerFQCN, int level, String msg, Throwable t) {
+    Level log4jLevel;
+    switch(level) {
+    case LocationAwareLogger.DEBUG_INT: 
+      log4jLevel = Level.DEBUG;
+      break;
+    case LocationAwareLogger.INFO_INT: 
+      log4jLevel = Level.INFO;
+      break;
+    case LocationAwareLogger.WARN_INT: 
+      log4jLevel = Level.WARN;
+      break;
+    case LocationAwareLogger.ERROR_INT: 
+      log4jLevel = Level.ERROR;
+      break;
+    default:
+      throw new IllegalStateException("Level number "+level+" is not recognized.");
+    }
+    logger.log(callerFQCN, log4jLevel, msg, t);
+  }
 }



More information about the slf4j-dev mailing list