[logback-dev] svn commit: r811 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback main/java/ch/qos/logback/classic main/java/ch/qos/logback/classic/util main/java/org/slf4j main/java/org/slf4j/impl test/java/ch/qos/logback/classic/util test/resources

noreply.ceki at qos.ch noreply.ceki at qos.ch
Sun Oct 29 22:15:05 CET 2006


Author: ceki
Date: Sun Oct 29 22:15:05 2006
New Revision: 811

Added:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/util/InitializationTest.java
   logback/trunk/logback-classic/src/test/resources/logback-test.xml
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/BasicConfigurator.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/Logger.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java
   logback/trunk/logback-classic/src/main/java/org/slf4j/LoggerFactory.java
   logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticLoggerBinder.java

Log:

ongoing work on auto configuration

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/BasicConfigurator.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/BasicConfigurator.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/BasicConfigurator.java	Sun Oct 29 22:15:05 2006
@@ -1,3 +1,13 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 1999-2006, 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 ch.qos.logback;
 
 import org.slf4j.LoggerFactory;
@@ -9,22 +19,22 @@
 
 public class BasicConfigurator {
 
-	public static void configure(LoggerContext lc) {
-		ConsoleAppender ca = new ConsoleAppender();
-		ca.setContext(lc);
-		ca.setName("console");
-		PatternLayout pl = new PatternLayout();
-		pl.setPattern("%-4relative [%thread] %-5level %class - %msg%n");
-		pl.start();
-
-		ca.setLayout(pl);
-		ca.start();
-		Logger rootLogger = lc.getLogger(LoggerContext.ROOT_NAME);
-		rootLogger.addAppender(ca);
-	}
-	
-	public static void configureDefaultContext() {
-		LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
-		configure(lc);
-	}
+  public static void configure(LoggerContext lc) {
+    ConsoleAppender ca = new ConsoleAppender();
+    ca.setContext(lc);
+    ca.setName("console");
+    PatternLayout pl = new PatternLayout();
+    pl.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
+    pl.start();
+
+    ca.setLayout(pl);
+    ca.start();
+    Logger rootLogger = lc.getLogger(LoggerContext.ROOT_NAME);
+    rootLogger.addAppender(ca);
+  }
+
+  public static void configureDefaultContext() {
+    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
+    configure(lc);
+  }
 }

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/Logger.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/Logger.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/Logger.java	Sun Oct 29 22:15:05 2006
@@ -337,6 +337,14 @@
     return childLogger;
   }
 
+  void recursiveReset() {
+    detachAndStopAllAppenders();
+    additive = true;
+    for(Logger childLogger: childrenList) {
+      childLogger.recursiveReset();
+    }
+  }
+  
   /**
    * The default size of child list arrays. The JDK 1.5 default is 10. We use a
    * smaller value to save a little space.

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java	Sun Oct 29 22:15:05 2006
@@ -154,4 +154,9 @@
   public LoggerContextRemoteView getLoggerContextRemoteView() {
     return loggerContextRemoteView;
   }
+  
+  public void reset() {
+    root.recursiveReset();
+   
+  }
 }

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java	Sun Oct 29 22:15:05 2006
@@ -0,0 +1,44 @@
+package ch.qos.logback.classic.util;
+
+import java.net.URL;
+
+import ch.qos.logback.BasicConfigurator;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.util.Loader;
+import ch.qos.logback.core.util.StatusPrinter;
+
+public class ContextInitializer {
+
+  final public static String AUTOCONFIG_FILE = "logback.xml";
+  final public static String TEST_AUTOCONFIG_FILE = "logback-test.xml";
+
+  public static void configureByResource(LoggerContext loggerContext, URL url)
+      throws JoranException {
+    if (url == null) {
+      throw new IllegalArgumentException("URL argument cannot be null");
+    }
+    JoranConfigurator configurator = new JoranConfigurator();
+    configurator.setContext(loggerContext);
+    configurator.doConfigure(url);
+  }
+
+  public static void autoConfig(LoggerContext loggerContext) {
+
+    URL url = Loader.getResource(AUTOCONFIG_FILE);
+    if (url == null) {
+      url = Loader.getResource(TEST_AUTOCONFIG_FILE);
+    }
+
+    if (url != null) {
+      try {
+        configureByResource(loggerContext, url);
+      } catch (JoranException je) {
+        StatusPrinter.print(loggerContext);
+      }
+    } else {
+      BasicConfigurator.configure(loggerContext);
+    }
+  }
+}

Modified: logback/trunk/logback-classic/src/main/java/org/slf4j/LoggerFactory.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/org/slf4j/LoggerFactory.java	(original)
+++ logback/trunk/logback-classic/src/main/java/org/slf4j/LoggerFactory.java	Sun Oct 29 22:15:05 2006
@@ -32,16 +32,11 @@
  */
 package org.slf4j;
 
-import java.net.URL;
-
-import org.slf4j.impl.StaticLoggerBinder;
 import org.slf4j.impl.Util;
 
-import ch.qos.logback.classic.joran.JoranConfigurator;
-import ch.qos.logback.core.Context;
-import ch.qos.logback.core.util.Loader;
-import ch.qos.logback.core.util.StatusPrinter;
-
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.util.ContextInitializer;
+ 
 /**
  * The <code>LoggerFactory</code> is a utility class producing Loggers for
  * various logging APIs, most notably for NLOG4J and JDK 1.4 logging. Other
@@ -60,7 +55,7 @@
  */
 public final class LoggerFactory {
 
-  static ILoggerFactory loggerFactory;
+  static LoggerContext loggerContext;
 
   // private constructor prevents instantiation
   private LoggerFactory() {
@@ -69,20 +64,13 @@
 
   static {
     try { 
-      loggerFactory = StaticLoggerBinder.SINGLETON.getLoggerFactory();
-      URL url = Loader.getResource("logback-classic.xml");
-      if (url != null) {
-        JoranConfigurator configurator = new JoranConfigurator();
-        configurator.setContext((Context)loggerFactory);
-        configurator.doConfigure(url);
-        StatusPrinter.print((Context)loggerFactory);
-      } else {
-        //TODO basic configuration??
-      }
+    loggerContext = new LoggerContext();
+    loggerContext.setName("default");
+      ContextInitializer.autoConfig(loggerContext);
     } catch (Exception e) {
       // we should never get here
       Util.reportFailure("Failed to instantiate logger ["
-          + StaticLoggerBinder.SINGLETON.getLoggerFactoryClassStr() + "]", e);
+          +  LoggerContext.class + "]", e);
     }
   }
 
@@ -95,7 +83,7 @@
    * @return logger
    */
   public static Logger getLogger(String name) {
-    return loggerFactory.getLogger(name);
+    return loggerContext.getLogger(name);
   }
 
   /**
@@ -107,7 +95,7 @@
    * @return logger
    */
   public static Logger getLogger(Class clazz) {
-    return loggerFactory.getLogger(clazz.getName());
+    return loggerContext.getLogger(clazz.getName());
   }
 
   /**
@@ -119,6 +107,6 @@
    * @return the ILoggerFactory instance in use
    */
   public static ILoggerFactory getILoggerFactory() {
-    return loggerFactory;
+    return loggerContext;
   }
 }

Modified: logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticLoggerBinder.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticLoggerBinder.java	(original)
+++ logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticLoggerBinder.java	Sun Oct 29 22:15:05 2006
@@ -1,3 +1,12 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 1999-2006, 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.slf4j.impl;
 
 import org.slf4j.ILoggerFactory;
@@ -8,8 +17,8 @@
 
 /**
  * 
- * The binding of {@link LoggerFactory} class with an actual instance of 
- * {@link ILoggerFactory} is performed using information returned by this class. 
+ * The binding of {@link LoggerFactory} class with an actual instance of
+ * {@link ILoggerFactory} is performed using information returned by this class.
  * 
  * @author <a href="http://www.qos.ch/log4j/">Ceki G&uuml;lc&uuml;</a>
  */
@@ -19,24 +28,26 @@
    * The unique instance of this class.
    */
   public static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
-  private static final String loggerFactoryClassStr = LoggerContext.class.getName();
+  private static final String loggerFactoryClassStr = LoggerContext.class
+      .getName();
 
-  /** The ILoggerFactory instance returned by the {@link #getLoggerFactory} method
-   * should always be the same object
+  /**
+   * The ILoggerFactory instance returned by the {@link #getLoggerFactory}
+   * method should always be the same object
    */
   private final ILoggerFactory loggerFactory;
-  
+
   private StaticLoggerBinder() {
-  	LoggerContext lc = new LoggerContext();
-  	 lc.setName("default");
+    LoggerContext lc = new LoggerContext();
+    lc.setName("default");
     loggerFactory = lc;
   }
-  
+
   public ILoggerFactory getLoggerFactory() {
     return loggerFactory;
   }
-  
+
   public String getLoggerFactoryClassStr() {
     return loggerFactoryClassStr;
-  }   
+  }
 }

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/util/InitializationTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/util/InitializationTest.java	Sun Oct 29 22:15:05 2006
@@ -0,0 +1,31 @@
+package ch.qos.logback.classic.util;
+
+import junit.framework.TestCase;
+
+
+import org.slf4j.LoggerFactory;
+
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.core.Appender;
+import ch.qos.logback.core.ConsoleAppender;
+
+public class InitializationTest extends TestCase {
+
+  protected void setUp() throws Exception {
+    super.setUp();
+  }
+
+  protected void tearDown() throws Exception {
+    super.tearDown();
+  }
+ 
+  public void test() {
+    org.slf4j.Logger logger = LoggerFactory.getLogger(InitializationTest.class);
+    logger.debug("Hello-didily-odily");
+    
+    Logger root = (Logger) LoggerFactory.getLogger("root");
+    Appender appender = root.getAppender("STDOUT");
+    assertNotNull(appender);
+    assertTrue(appender instanceof ConsoleAppender);
+  }
+}

Added: logback/trunk/logback-classic/src/test/resources/logback-test.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/resources/logback-test.xml	Sun Oct 29 22:15:05 2006
@@ -0,0 +1,15 @@
+<configuration>
+
+  <appender name="STDOUT"
+    class="ch.qos.logback.core.ConsoleAppender">
+    <layout class="ch.qos.logback.classic.PatternLayout">
+      <param name="Pattern"
+        value="TEST %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />
+    </layout>
+  </appender>
+
+  <root>
+    <level value="debug" />
+    <appender-ref ref="STDOUT" />
+  </root>
+</configuration>
\ No newline at end of file



More information about the logback-dev mailing list