[slf4j-dev] svn commit: r1313 - in slf4j/trunk/log4j-over-slf4j: . src/main/java/org/apache/log4j src/test/java src/test/java/org src/test/java/org/apache src/test/java/org/apache/log4j src/test/java/org/dummy
ceki at slf4j.org
ceki at slf4j.org
Fri Apr 17 18:42:18 CEST 2009
Author: ceki
Date: Fri Apr 17 18:42:18 2009
New Revision: 1313
Added:
slf4j/trunk/log4j-over-slf4j/src/test/java/
slf4j/trunk/log4j-over-slf4j/src/test/java/org/
slf4j/trunk/log4j-over-slf4j/src/test/java/org/apache/
slf4j/trunk/log4j-over-slf4j/src/test/java/org/apache/log4j/
slf4j/trunk/log4j-over-slf4j/src/test/java/org/apache/log4j/Trivial.java
slf4j/trunk/log4j-over-slf4j/src/test/java/org/dummy/
slf4j/trunk/log4j-over-slf4j/src/test/java/org/dummy/Bug131.java
slf4j/trunk/log4j-over-slf4j/src/test/java/org/dummy/ListHandler.java
Modified:
slf4j/trunk/log4j-over-slf4j/pom.xml
slf4j/trunk/log4j-over-slf4j/src/main/java/org/apache/log4j/Category.java
slf4j/trunk/log4j-over-slf4j/src/main/java/org/apache/log4j/Logger.java
Log:
- Added support for location information. This fixes bug 131.
Modified: slf4j/trunk/log4j-over-slf4j/pom.xml
==============================================================================
--- slf4j/trunk/log4j-over-slf4j/pom.xml (original)
+++ slf4j/trunk/log4j-over-slf4j/pom.xml Fri Apr 17 18:42:18 2009
@@ -36,7 +36,12 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
- </dependency>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-jdk14</artifactId>
+ </dependency>
</dependencies>
Modified: slf4j/trunk/log4j-over-slf4j/src/main/java/org/apache/log4j/Category.java
==============================================================================
--- slf4j/trunk/log4j-over-slf4j/src/main/java/org/apache/log4j/Category.java (original)
+++ slf4j/trunk/log4j-over-slf4j/src/main/java/org/apache/log4j/Category.java Fri Apr 17 18:42:18 2009
@@ -23,33 +23,34 @@
/**
* <p>
* This class is a minimal implementation of the original
- * <code>org.apache.log4j.Category</code> class (as found in log4j 1.2)
- * by delegation of all calls to a {@link org.slf4j.Logger.Logger} instance.
+ * <code>org.apache.log4j.Category</code> class (as found in log4j 1.2) by
+ * delegation of all calls to a {@link org.slf4j.Logger.Logger} instance.
* </p>
*
* <p>
- * Log4j's <code>trace</code>, <code>debug()</code>, <code>info()</code>,
- * <code>warn()</code>, <code>error()</code> printing methods are directly
- * mapped to their SLF4J equivalents. Log4j's <code>fatal()</code>
- * printing method is mapped to SLF4J's <code>error()</code> method
- * with a FATAL marker.
+ * Log4j's <code>trace</code>, <code>debug()</code>, <code>info()</code>,
+ * <code>warn()</code>, <code>error()</code> printing methods are directly
+ * mapped to their SLF4J equivalents. Log4j's <code>fatal()</code> printing
+ * method is mapped to SLF4J's <code>error()</code> method with a FATAL marker.
*
* @author Sébastien Pennec
* @author Ceki Gülcü
*/
public class Category {
+ private static final String CATEGORY_FQCN = Category.class.getName();
+
private String name;
protected org.slf4j.Logger slf4jLogger;
private org.slf4j.spi.LocationAwareLogger locationAwareLogger;
-
+
private static Marker FATAL_MARKER = MarkerFactory.getMarker("FATAL");
Category(String name) {
this.name = name;
slf4jLogger = LoggerFactory.getLogger(name);
- if(slf4jLogger instanceof LocationAwareLogger) {
+ if (slf4jLogger instanceof LocationAwareLogger) {
locationAwareLogger = (LocationAwareLogger) slf4jLogger;
}
}
@@ -80,24 +81,24 @@
* @return
*/
public Level getEffectiveLevel() {
- if(slf4jLogger.isTraceEnabled()) {
+ if (slf4jLogger.isTraceEnabled()) {
return Level.TRACE;
}
- if(slf4jLogger.isDebugEnabled()) {
+ if (slf4jLogger.isDebugEnabled()) {
return Level.DEBUG;
}
- if(slf4jLogger.isInfoEnabled()) {
+ if (slf4jLogger.isInfoEnabled()) {
return Level.INFO;
}
- if(slf4jLogger.isWarnEnabled()) {
+ if (slf4jLogger.isWarnEnabled()) {
return Level.WARN;
}
return Level.ERROR;
}
/**
- * Returns the assigned {@link Level}, if any, for this Category.
- * This implementation always returns null.
+ * Returns the assigned {@link Level}, if any, for this Category. This
+ * implementation always returns null.
*
* @return Level - the assigned Level, can be <code>null</code>.
*/
@@ -105,50 +106,50 @@
return null;
}
-
/**
* @deprecated Please use {@link #getLevel} instead.
- */
+ */
final public Level getPriority() {
return null;
}
-
+
/**
- * Delegates to {@link org.slf4j.Logger#isDebugEnabled} method in SLF4J
+ * Delegates to {@link org.slf4j.Logger#isDebugEnabled} method in SLF4J
*/
public boolean isDebugEnabled() {
return slf4jLogger.isDebugEnabled();
}
/**
- * Delegates to {@link org.slf4j.Logger#isInfoEnabled} method in SLF4J
+ * Delegates to {@link org.slf4j.Logger#isInfoEnabled} method in SLF4J
*/
public boolean isInfoEnabled() {
return slf4jLogger.isInfoEnabled();
}
/**
- * Delegates tob {@link org.slf4j.Logger#isWarnEnabled} method in SLF4J
+ * Delegates tob {@link org.slf4j.Logger#isWarnEnabled} method in SLF4J
*/
public boolean isWarnEnabled() {
return slf4jLogger.isWarnEnabled();
}
-
+
/**
* Delegates to {@link org.slf4j.Logger#isErrorEnabled} method in SLF4J
*/
public boolean isErrorEnabled() {
return slf4jLogger.isErrorEnabled();
}
-
/**
- * Determines whether the priority passed as parameter is enabled in
- * the underlying SLF4J logger. Each log4j priority is mapped directly to
- * its SLF4J equivalent, except for FATAL which is mapped as ERROR.
+ * Determines whether the priority passed as parameter is enabled in the
+ * underlying SLF4J logger. Each log4j priority is mapped directly to its
+ * SLF4J equivalent, except for FATAL which is mapped as ERROR.
*
- * @param p the priority to check against
- * @return true if this logger is enabled for the given level, false otherwise.
+ * @param p
+ * the priority to check against
+ * @return true if this logger is enabled for the given level, false
+ * otherwise.
*/
public boolean isEnabledFor(Priority p) {
switch (p.level) {
@@ -168,104 +169,117 @@
return false;
}
-
+ void innerLog(Marker marker, String fqcn, int level, Object message,
+ Throwable t) {
+ String m = convertToString(message);
+ if (locationAwareLogger != null) {
+ locationAwareLogger.log(marker, fqcn, level, m, t);
+ } else {
+ switch (level) {
+ case LocationAwareLogger.TRACE_INT:
+ slf4jLogger.trace(marker, m);
+ break;
+ case LocationAwareLogger.DEBUG_INT:
+ slf4jLogger.debug(marker, m);
+ break;
+ case LocationAwareLogger.INFO_INT:
+ slf4jLogger.info(marker, m);
+ break;
+ case LocationAwareLogger.WARN_INT:
+ slf4jLogger.warn(marker, m);
+ break;
+ case LocationAwareLogger.ERROR_INT:
+ slf4jLogger.error(marker, m);
+ break;
+ }
+ }
+ }
+
/**
- * Delegates to {@link org.slf4j.Logger#debug(String)} method of
- * SLF4J.
+ * Delegates to {@link org.slf4j.Logger#debug(String)} method of SLF4J.
*/
public void debug(Object message) {
- // casting to String as SLF4J only accepts String instances, not Object
- // instances.
- slf4jLogger.debug(convertToString(message));
+ innerLog(null, CATEGORY_FQCN, LocationAwareLogger.DEBUG_INT, message, null);
}
/**
- * Delegates to {@link org.slf4j.Logger#debug(String,Throwable)}
- * method in SLF4J.
+ * Delegates to {@link org.slf4j.Logger#debug(String,Throwable)} method in
+ * SLF4J.
*/
public void debug(Object message, Throwable t) {
- slf4jLogger.debug(convertToString(message), t);
+ innerLog(null, CATEGORY_FQCN, LocationAwareLogger.DEBUG_INT, message, t);
}
/**
- * Delegates to {@link org.slf4j.Logger#info(String)}
- * method in SLF4J.
+ * Delegates to {@link org.slf4j.Logger#info(String)} method in SLF4J.
*/
public void info(Object message) {
- slf4jLogger.info(convertToString(message));
+ innerLog(null, CATEGORY_FQCN, LocationAwareLogger.INFO_INT, message, null);
}
/**
- * Delegates to {@link org.slf4j.Logger#info(String,Throwable)}
- * method in SLF4J.
+ * Delegates to {@link org.slf4j.Logger#info(String,Throwable)} method in
+ * SLF4J.
*/
public void info(Object message, Throwable t) {
- slf4jLogger.info(convertToString(message), t);
+ innerLog(null, CATEGORY_FQCN, LocationAwareLogger.INFO_INT, message, t);
}
/**
- * Delegates to {@link org.slf4j.Logger#warn(String)}
- * method in SLF4J.
+ * Delegates to {@link org.slf4j.Logger#warn(String)} method in SLF4J.
*/
public void warn(Object message) {
- slf4jLogger.warn(convertToString(message));
+ innerLog(null, CATEGORY_FQCN, LocationAwareLogger.WARN_INT, message, null);
}
/**
- * Delegates to {@link org.slf4j.Logger#warn(String,Throwable)}
- * method in SLF4J.
+ * Delegates to {@link org.slf4j.Logger#warn(String,Throwable)} method in
+ * SLF4J.
*/
public void warn(Object message, Throwable t) {
- slf4jLogger.warn(convertToString(message), t);
+ innerLog(null, CATEGORY_FQCN, LocationAwareLogger.WARN_INT, message, t);
}
-
/**
- * Delegates to {@link org.slf4j.Logger#error(String)}
- * method in SLF4J.
+ * Delegates to {@link org.slf4j.Logger#error(String)} method in SLF4J.
*/
public void error(Object message) {
- slf4jLogger.error(convertToString(message));
+ innerLog(null, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT, message, null);
}
/**
- * Delegates to {@link org.slf4j.Logger#error(String,Throwable)}
- * method in SLF4J.
+ * Delegates to {@link org.slf4j.Logger#error(String,Throwable)} method in
+ * SLF4J.
*/
public void error(Object message, Throwable t) {
- slf4jLogger.error(convertToString(message), t);
+ innerLog(null, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT, message, t);
}
/**
- * Delegates to {@link org.slf4j.Logger#error(String)}
- * method in SLF4J.
+ * Delegates to {@link org.slf4j.Logger#error(String)} method in SLF4J.
*/
public void fatal(Object message) {
- slf4jLogger.error(FATAL_MARKER, convertToString(message));
+ innerLog(FATAL_MARKER, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT, message, null);
}
/**
- * Delegates to {@link org.slf4j.Logger#error(String,Throwable)}
- * method in SLF4J. In addition, the call is marked with a marker named "FATAL".
+ * Delegates to {@link org.slf4j.Logger#error(String,Throwable)} method in
+ * SLF4J. In addition, the call is marked with a marker named "FATAL".
*/
public void fatal(Object message, Throwable t) {
- slf4jLogger.error(FATAL_MARKER, convertToString(message), t);
+ innerLog(FATAL_MARKER, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT, message, t);
}
public void log(String FQCN, Priority p, Object msg, Throwable t) {
int levelInt = priorityToLevelInt(p);
- if(locationAwareLogger != null) {
- if(msg != null) {
- locationAwareLogger.log(null, FQCN, levelInt, msg.toString(), t);
- } else {
- locationAwareLogger.log(null, FQCN, levelInt, null, t);
- }
+ if (locationAwareLogger != null) {
+ locationAwareLogger.log(null, FQCN, levelInt, convertToString(msg), t);
} else {
- throw new UnsupportedOperationException("The logger ["+slf4jLogger+"] does not seem to be location aware.");
+ throw new UnsupportedOperationException("The logger [" + slf4jLogger
+ + "] does not seem to be location aware.");
}
-
}
-
+
private int priorityToLevelInt(Priority p) {
switch (p.level) {
case Level.TRACE_INT:
@@ -284,10 +298,10 @@
throw new IllegalStateException("Unknown Priority " + p);
}
}
-
+
protected final String convertToString(Object message) {
if (message == null) {
- return (String)message;
+ return (String) message;
} else {
return message.toString();
}
Modified: slf4j/trunk/log4j-over-slf4j/src/main/java/org/apache/log4j/Logger.java
==============================================================================
--- slf4j/trunk/log4j-over-slf4j/src/main/java/org/apache/log4j/Logger.java (original)
+++ slf4j/trunk/log4j-over-slf4j/src/main/java/org/apache/log4j/Logger.java Fri Apr 17 18:42:18 2009
@@ -16,6 +16,8 @@
package org.apache.log4j;
+import org.slf4j.spi.LocationAwareLogger;
+
/**
* <p>
* This class is a minimal implementation of the original
@@ -26,7 +28,9 @@
* @author Ceki Gülcü
* */
public class Logger extends Category {
-
+
+ private static final String LOGGER_FQCN = Logger.class.getName();
+
Logger(String name) {
super(name);
}
@@ -61,9 +65,7 @@
* Delegates to {@link org.slf4j.Logger#trace(String)} method in SLF4J.
*/
public void trace(Object message) {
- // casting to String as SLF4J only accepts String instances, not Object
- // instances.
- slf4jLogger.trace(convertToString(message));
+ innerLog(null, LOGGER_FQCN, LocationAwareLogger.TRACE_INT, message, null);
}
/**
@@ -71,7 +73,7 @@
* method in SLF4J.
*/
public void trace(Object message, Throwable t) {
- slf4jLogger.trace(convertToString(message), t);
+ innerLog(null, LOGGER_FQCN, LocationAwareLogger.TRACE_INT, message, null);
}
}
Added: slf4j/trunk/log4j-over-slf4j/src/test/java/org/apache/log4j/Trivial.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/log4j-over-slf4j/src/test/java/org/apache/log4j/Trivial.java Fri Apr 17 18:42:18 2009
@@ -0,0 +1,27 @@
+package org.apache.log4j;
+
+import org.apache.log4j.Logger;
+
+import junit.framework.TestCase;
+
+public class Trivial extends TestCase {
+
+ public void testSmoke() {
+ Logger l = Logger.getLogger("a");
+ l.trace("t");
+ l.debug("d");
+ l.info("i");
+ l.warn("w");
+ l.error("e");
+ l.fatal("f");
+
+ Exception e = new Exception("testing");
+ l.trace("t", e);
+ l.debug("d", e);
+ l.info("i", e);
+ l.warn("w", e);
+ l.error("e", e);
+ l.fatal("f", e);
+ }
+
+}
Added: slf4j/trunk/log4j-over-slf4j/src/test/java/org/dummy/Bug131.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/log4j-over-slf4j/src/test/java/org/dummy/Bug131.java Fri Apr 17 18:42:18 2009
@@ -0,0 +1,44 @@
+package org.dummy;
+
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.Category;
+import org.apache.log4j.Logger;
+
+public class Bug131 extends TestCase {
+
+ public void testBug131() {
+
+ ListHandler listHandler = new ListHandler();
+ java.util.logging.Logger root = java.util.logging.Logger.getLogger("");
+ root.addHandler(listHandler);
+ root.setLevel(Level.FINEST);
+ Logger log4jLogger = Logger.getLogger("a");
+ Category log4jCategory = Logger.getLogger("b");
+
+ int n = 0;
+
+ log4jLogger.trace("msg" +(n++));
+ log4jLogger.debug("msg" +(n++));
+ log4jLogger.info("msg" +(n++));
+ log4jLogger.warn("msg" +(n++));
+ log4jLogger.error("msg" +(n++));
+ log4jLogger.fatal("msg" +(n++));
+
+ log4jCategory.debug("msg" +(n++));
+ log4jCategory.info("msg" +(n++));
+ log4jCategory.warn("msg" +(n++));
+ log4jCategory.error("msg" +(n++));
+ log4jCategory.fatal("msg" +(n++));
+
+ assertEquals(n, listHandler.list.size());
+
+ for(int i = 0; i < n; i++) {
+ LogRecord logRecord = (LogRecord) listHandler.list.get(i);
+ assertEquals("testBug131", logRecord.getSourceMethodName());
+ }
+ }
+}
Added: slf4j/trunk/log4j-over-slf4j/src/test/java/org/dummy/ListHandler.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/log4j-over-slf4j/src/test/java/org/dummy/ListHandler.java Fri Apr 17 18:42:18 2009
@@ -0,0 +1,25 @@
+package org.dummy;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Handler;
+import java.util.logging.LogRecord;
+
+public class ListHandler extends Handler {
+
+ List list = new ArrayList();
+
+ public void close() throws SecurityException {
+
+ }
+
+ public void flush() {
+
+ }
+
+ public void publish(LogRecord logRecord) {
+ logRecord.getSourceClassName();
+ list.add(logRecord);
+ }
+
+}
More information about the slf4j-dev
mailing list