[logback-dev] svn commit: r2053 - in logback/trunk: logback-classic logback-classic/src/main/java/ch/qos/logback/classic/joran logback-classic/src/main/java/ch/qos/logback/classic/joran/action logback-classic/src/main/java/org/slf4j/impl logback-classic/src/test/input/joran logback-classic/src/test/java/ch/qos/logback/classic/joran logback-core/src/main/java/ch/qos/logback/core logback-core/src/main/java/ch/qos/logback/core/joran/action logback-core/src/test/java/ch/qos/logback/core

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Dec 4 17:37:46 CET 2008


Author: ceki
Date: Thu Dec  4 17:37:45 2008
New Revision: 2053

Added:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/ContextNameAction.java
   logback/trunk/logback-classic/src/test/input/joran/contextRename.xml
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/ContextBaseTest.java
Modified:
   logback/trunk/logback-classic/pom.xml
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/JoranConfigurator.java
   logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticLoggerBinder.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/ContextBase.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/CoreConstants.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedBasicPropertyIA.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/PackageTest.java

Log:
- Allow for the logger context name to be changed from a config file + related test cases.
  See also LBCLASSIC-43
- cosmetic changes

Modified: logback/trunk/logback-classic/pom.xml
==============================================================================
--- logback/trunk/logback-classic/pom.xml	(original)
+++ logback/trunk/logback-classic/pom.xml	Thu Dec  4 17:37:45 2008
@@ -48,7 +48,7 @@
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-ext</artifactId>
-      <scope>compile</scope>
+      <scope>test</scope>
     </dependency>
     
     <dependency>

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/JoranConfigurator.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/JoranConfigurator.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/JoranConfigurator.java	Thu Dec  4 17:37:45 2008
@@ -12,6 +12,7 @@
 
 import ch.qos.logback.classic.joran.action.ConfigurationAction;
 import ch.qos.logback.classic.joran.action.ConsolePluginAction;
+import ch.qos.logback.classic.joran.action.ContextNameAction;
 import ch.qos.logback.classic.joran.action.EvaluatorAction;
 import ch.qos.logback.classic.joran.action.JMXConfiguratorAction;
 import ch.qos.logback.classic.joran.action.LayoutAction;
@@ -43,6 +44,7 @@
 
     rs.addRule(new Pattern("configuration"), new ConfigurationAction());
 
+    rs.addRule(new Pattern("configuration/contextName"), new ContextNameAction());
     rs.addRule(new Pattern("*/evaluator"), new EvaluatorAction());
     rs.addRule(new Pattern("*/evaluator/matcher"),
         new MatcherAction());

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/ContextNameAction.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/ContextNameAction.java	Thu Dec  4 17:37:45 2008
@@ -0,0 +1,37 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-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 ch.qos.logback.classic.joran.action;
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.spi.InterpretationContext;
+
+public class ContextNameAction extends Action {
+
+  public void begin(InterpretationContext ec, String name, Attributes attributes) {
+  }
+
+  public void body(InterpretationContext ec, String body) {
+
+    String finalBody = ec.subst(body);
+    addInfo("Setting logger context name as [" + finalBody + "]");
+    try {
+      context.setName(finalBody);
+    } catch (IllegalStateException e) {
+      addError("Failed to rename context [" + context.getName() + "] as ["
+          + finalBody + "]", e);
+    }
+  }
+
+  public void end(InterpretationContext ec, String name) {
+  }
+}

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	Thu Dec  4 17:37:45 2008
@@ -62,7 +62,7 @@
 
   
   private StaticLoggerBinder() {
-    defaultLoggerContext.setName("default");
+    defaultLoggerContext.setName(CoreConstants.DEFAULT_CONTEXT_NAME);
   }
 
   public static StaticLoggerBinder getSingleton() {

Added: logback/trunk/logback-classic/src/test/input/joran/contextRename.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/input/joran/contextRename.xml	Thu Dec  4 17:37:45 2008
@@ -0,0 +1,3 @@
+<configuration>
+  <contextName>wombat</contextName>
+</configuration>
\ No newline at end of file

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java	Thu Dec  4 17:37:45 2008
@@ -25,6 +25,7 @@
 import ch.qos.logback.classic.turbo.NOPTurboFilter;
 import ch.qos.logback.classic.turbo.TurboFilter;
 import ch.qos.logback.classic.util.TeztConstants;
+import ch.qos.logback.core.CoreConstants;
 import ch.qos.logback.core.joran.spi.JoranException;
 import ch.qos.logback.core.read.ListAppender;
 import ch.qos.logback.core.util.StatusPrinter;
@@ -99,11 +100,17 @@
   @Test
   public void testStatusListener() throws JoranException {
     configure(TeztConstants.TEST_DIR_PREFIX + "input/joran/statusListener.xml");
-
     StatusPrinter.print(loggerContext);
   }
 
   @Test
+  public void contextRename() throws JoranException {
+    loggerContext.setName(CoreConstants.DEFAULT_CONTEXT_NAME);
+    configure(TeztConstants.TEST_DIR_PREFIX + "input/joran/contextRename.xml");
+    assertEquals("wombat", loggerContext.getName());
+  }
+  
+  @Test
   public void testEval() throws JoranException {
     configure(TeztConstants.TEST_DIR_PREFIX + "input/joran/callerData.xml");
 

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/ContextBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/ContextBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/ContextBase.java	Thu Dec  4 17:37:45 2008
@@ -33,15 +33,14 @@
    * context is initialized with a {@link BasicStatusManager}. A null value for
    * the 'statusManager' argument is not allowed.
    * 
-   * <p>
-   * A malicious attacker can set the status manager to a dummy instance,
+   * <p> A malicious attacker can set the status manager to a dummy instance,
    * disabling internal error reporting.
    * 
    * @param statusManager
    *                the new status manager
    */
-  // added in response to http://jira.qos.ch/browse/LBCORE-35
   public void setStatusManager(StatusManager statusManager) {
+    // this method was added in response to http://jira.qos.ch/browse/LBCORE-35
     if (sm == null) {
       throw new IllegalArgumentException("null StatusManager not allowed");
     }
@@ -72,10 +71,23 @@
     return name;
   }
 
-  public void setName(String name) {
-    if (this.name != null) {
+  /**
+   * The context name can be set only if it is not already set, or if the
+   * current name is the default context name, namely "default", or if the
+   * current name and the old name are the same.
+   * 
+   * @throws IllegalStateException
+   *                 if the context already has a name, other than "default".
+   */
+  public void setName(String name) throws IllegalStateException {
+    if (name != null && name.equals(this.name)) {
+      return; // idempotent naming
+    }
+    if (this.name == null
+        || CoreConstants.DEFAULT_CONTEXT_NAME.equals(this.name)) {
+      this.name = name;
+    } else {
       throw new IllegalStateException("Context has been already given a name");
     }
-    this.name = name;
   }
 }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/CoreConstants.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/CoreConstants.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/CoreConstants.java	Thu Dec  4 17:37:45 2008
@@ -11,6 +11,10 @@
   public static final String CODES_URL = "http://logback.qos.ch/codes.html";
   
   /**
+   * The default context name.
+   */
+  public static final String DEFAULT_CONTEXT_NAME = "default";
+  /**
    * Customized pattern conversion rules are stored under this key in the
    * {@link Context} object store.
    */

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedBasicPropertyIA.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedBasicPropertyIA.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedBasicPropertyIA.java	Thu Dec  4 17:37:45 2008
@@ -1,13 +1,12 @@
 /**
- * LOGBack: the generic, reliable, fast and flexible logging framework.
+ * Logback: the generic, reliable, fast and flexible logging framework.
  * 
- * Copyright (C) 1999-2006, QOS.ch
+ * Copyright (C) 2000-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 ch.qos.logback.core.joran.action;
 
 import java.util.Stack;

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/ContextBaseTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/ContextBaseTest.java	Thu Dec  4 17:37:45 2008
@@ -0,0 +1,43 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-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 ch.qos.logback.core;
+
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+public class ContextBaseTest {
+
+  ContextBase context = new ContextBase();
+
+  @Test
+  public void renameDefault() {
+    context.setName(CoreConstants.DEFAULT_CONTEXT_NAME);
+    context.setName("hello");
+  }
+
+  
+  @Test
+  public void idempotentNameTest() {
+    context.setName("hello");
+    context.setName("hello");
+  }
+
+  @Test
+  public void renameTest() {
+    context.setName("hello");
+    try {
+      context.setName("x");
+      fail("renaming is not allowed");
+    } catch (IllegalStateException ise) {
+    }
+  }
+
+}

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/PackageTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/PackageTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/PackageTest.java	Thu Dec  4 17:37:45 2008
@@ -5,6 +5,6 @@
 import org.junit.runners.Suite.SuiteClasses;
 
 @RunWith(Suite.class)
- at SuiteClasses({WriterAppenderTest.class})
+ at SuiteClasses( { ContextBaseTest.class, WriterAppenderTest.class })
 public class PackageTest {
 }


More information about the logback-dev mailing list