[logback-dev] svn commit: r1615 - in logback/trunk/logback-classic: . src/main/java/ch/qos/logback/classic/spi src/test/java/org src/test/java/org/dummy

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Jan 31 11:32:17 CET 2008


Author: ceki
Date: Thu Jan 31 11:32:17 2008
New Revision: 1615

Added:
   logback/trunk/logback-classic/src/test/java/org/
   logback/trunk/logback-classic/src/test/java/org/dummy/
   logback/trunk/logback-classic/src/test/java/org/dummy/DummyLBAppender.java
   logback/trunk/logback-classic/src/test/java/org/dummy/Log4jInvocation.java
Modified:
   logback/trunk/logback-classic/pom.xml
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/CallerData.java

Log:
- fix bug 66 reported by Franck Routier on the SLF4J site. See 
  http://bugzilla.slf4j.org/show_bug.cgi?id=66 for more details.

  In short, the caller data was not correctly extracted when going
  through log4j-over-slf4j.


Modified: logback/trunk/logback-classic/pom.xml
==============================================================================
--- logback/trunk/logback-classic/pom.xml	(original)
+++ logback/trunk/logback-classic/pom.xml	Thu Jan 31 11:32:17 2008
@@ -46,6 +46,13 @@
     </dependency>
 
     <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>log4j-over-slf4j</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+
+    <dependency>
       <groupId>dom4j</groupId>
       <artifactId>dom4j</artifactId>
       <scope>test</scope>

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/CallerData.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/CallerData.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/CallerData.java	Thu Jan 31 11:32:17 2008
@@ -26,6 +26,9 @@
    */
   public static final String NA = "?";
 
+  // All logger call's in log4j-over-slf4j use the Category class
+  private static final String LOG4J_CATEGORY = "org.apache.log4j.Category";
+  
   /**
    * When caller information is not available this constant is used for the line
    * number.
@@ -91,7 +94,7 @@
     
     int found = LINE_NA;
     for (int i = 0; i < steArray.length; i++) {
-      if(steArray[i].getClassName().equals(fqnOfInvokingClass)) {
+      if(isDirectlyInvokingClass(steArray[i].getClassName(), fqnOfInvokingClass)) {
         // the caller is assumed to be the next stack frame, hence the +1.
         found = i + 1;
       } else {
@@ -113,6 +116,15 @@
     return callerDataArray;
   }
   
+  public static boolean isDirectlyInvokingClass(String currentClass, String fqnOfInvokingClass) {
+    // the check for org.apachje.log4j.Category class is intended to support log4j-over-slf4j
+    // it solves http://bugzilla.slf4j.org/show_bug.cgi?id=66
+    if(currentClass.equals(fqnOfInvokingClass) || currentClass.equals(LOG4J_CATEGORY)) {
+      return true;
+    } else {
+      return false;
+    }
+  }
   
   public boolean equals(Object o) {
     // LogLog.info("equals called");

Added: logback/trunk/logback-classic/src/test/java/org/dummy/DummyLBAppender.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/org/dummy/DummyLBAppender.java	Thu Jan 31 11:32:17 2008
@@ -0,0 +1,41 @@
+/**
+ * LOGBack: the generic, reliable, fast and flexible logging framework.
+ *
+ * Copyright (C) 1999-2008, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation.
+ */
+package org.dummy;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import ch.qos.logback.classic.PatternLayout;
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.core.AppenderBase;
+
+public class DummyLBAppender extends AppenderBase<LoggingEvent> {
+
+  public List<LoggingEvent> list = new ArrayList<LoggingEvent>();
+  public List<String> stringList = new ArrayList<String>();
+  
+  PatternLayout layout;
+  
+  DummyLBAppender() {
+    this(null);
+  }
+  
+  DummyLBAppender(PatternLayout layout) {
+    this.layout = layout;
+  }
+  
+  protected void append(LoggingEvent e) {
+    list.add(e);
+    if(layout != null) {
+      String s = layout.doLayout(e);
+      stringList.add(s);
+    }
+  }
+}

Added: logback/trunk/logback-classic/src/test/java/org/dummy/Log4jInvocation.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/org/dummy/Log4jInvocation.java	Thu Jan 31 11:32:17 2008
@@ -0,0 +1,72 @@
+package org.dummy;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.log4j.Logger;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.LoggerFactory;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.PatternLayout;
+import ch.qos.logback.classic.spi.LoggingEvent;
+
+public class Log4jInvocation {
+
+  static final String HELLO = "Hello";
+
+  DummyLBAppender listAppender;
+  LoggerContext lc;
+  ch.qos.logback.classic.Logger rootLogger;
+  
+  @Before
+  public void fixture() {
+    lc = (LoggerContext) LoggerFactory.getILoggerFactory();
+    lc.shutdownAndReset();
+
+    listAppender = new DummyLBAppender();
+    listAppender.setContext(lc);
+    listAppender.start();
+    rootLogger = lc.getLogger("root");
+    rootLogger.addAppender(listAppender);
+  }
+
+  @Test
+  public void basic() {
+    assertEquals(0, listAppender.list.size());
+
+    Logger logger = Logger.getLogger("basic-test");
+    logger.debug(HELLO);
+
+    assertEquals(1, listAppender.list.size());
+    LoggingEvent event = (LoggingEvent) listAppender.list.get(0);
+    assertEquals(HELLO, event.getMessage());
+  }
+
+  @Test
+  public void callerData() {
+    assertEquals(0, listAppender.list.size());
+
+    PatternLayout pl = new PatternLayout();
+    pl.setPattern("%-5level [%class] %logger - %msg");
+    pl.setContext(lc);
+    pl.start();
+    listAppender.layout = pl;
+
+    Logger logger = Logger.getLogger("basic-test");
+    logger.trace("none");
+    assertEquals(0, listAppender.list.size());
+    
+    rootLogger.setLevel(Level.TRACE);
+    logger.trace(HELLO);
+    assertEquals(1, listAppender.list.size());
+
+    LoggingEvent event = (LoggingEvent) listAppender.list.get(0);
+    assertEquals(HELLO, event.getMessage());
+
+    assertEquals(1, listAppender.stringList.size());
+    assertEquals("TRACE [" + Log4jInvocation.class.getName()
+        + "] basic-test - Hello", listAppender.stringList.get(0));
+  }
+}



More information about the logback-dev mailing list