[slf4j-dev] svn commit: r1236 - in slf4j/trunk: slf4j-ext slf4j-ext/src/main/java/org/slf4j/ext slf4j-ext/src/test/java/org/slf4j/dummyExt slf4j-site/src/site/pages

rgoers at slf4j.org rgoers at slf4j.org
Sat Nov 15 23:10:29 CET 2008


Author: rgoers
Date: Sat Nov 15 23:10:29 2008
New Revision: 1236

Added:
   slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/MDCStrLookup.java
   slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/MDCStrLookupTest.java
   slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/PackageTest.java
Modified:
   slf4j/trunk/slf4j-ext/pom.xml
   slf4j/trunk/slf4j-site/src/site/pages/extensions.html

Log:
Add MDCStrLookup and corresponding test


Modified: slf4j/trunk/slf4j-ext/pom.xml
==============================================================================
--- slf4j/trunk/slf4j-ext/pom.xml	(original)
+++ slf4j/trunk/slf4j-ext/pom.xml	Sat Nov 15 23:10:29 2008
@@ -34,6 +34,12 @@
       <artifactId>javassist</artifactId>
       <version>3.4.GA</version>
     </dependency>
+    <dependency>
+      <groupId>commons-lang</groupId>
+      <artifactId>commons-lang</artifactId>
+      <version>2.4</version>
+      <optional>true</optional>
+    </dependency>
   </dependencies>
 
   <build>

Added: slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/MDCStrLookup.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/MDCStrLookup.java	Sat Nov 15 23:10:29 2008
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2004-2007 QOS.ch
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+package org.slf4j.ext;
+
+import org.apache.commons.lang.text.StrLookup;
+import org.slf4j.MDC;
+
+
+/**
+ * This class can be used with the Commons Lang StrSubstitutor to replace
+ * tokens that occur in Strings with their values in the MDC.
+ *
+ * @author Ralph Goers
+ */
+public class MDCStrLookup extends StrLookup {
+    /**
+     * Looks up up a value in the MDC.
+     *
+     * @param key the key to be looked up, may be null
+     * @return the matching value, null if no match
+     */
+    public String lookup(String key) {
+        if (key == null) {
+            return null;
+        }
+        return MDC.get(key);
+    }
+}

Added: slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/MDCStrLookupTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/MDCStrLookupTest.java	Sat Nov 15 23:10:29 2008
@@ -0,0 +1,33 @@
+package org.slf4j.dummyExt;
+
+import junit.framework.TestCase;
+
+import org.slf4j.MDC;
+import org.slf4j.ext.MDCStrLookup;
+
+public class MDCStrLookupTest extends TestCase {
+
+
+    public MDCStrLookupTest(String name) {
+        super(name);
+    }
+
+    public void setUp() throws Exception {
+        super.setUp();
+    }
+
+    public void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testLookup() throws Exception {
+        MDC.put("key", "value");
+        MDC.put("number", "2");
+        MDCStrLookup lookup = new MDCStrLookup();
+        assertEquals("value", lookup.lookup("key"));
+        assertEquals("2", lookup.lookup("number"));
+        assertEquals(null, lookup.lookup(null));
+        assertEquals(null, lookup.lookup(""));
+        assertEquals(null, lookup.lookup("other"));
+    }
+}

Added: slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/PackageTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/PackageTest.java	Sat Nov 15 23:10:29 2008
@@ -0,0 +1,13 @@
+package org.slf4j.dummyExt;
+
+import junit.framework.*;
+
+public class PackageTest extends TestCase {
+
+  public static Test suite() {
+    TestSuite suite = new TestSuite();
+    suite.addTestSuite(MDCStrLookupTest.class);
+    suite.addTestSuite(XLoggerTest.class);
+    return suite;
+  }
+}

Modified: slf4j/trunk/slf4j-site/src/site/pages/extensions.html
==============================================================================
--- slf4j/trunk/slf4j-site/src/site/pages/extensions.html	(original)
+++ slf4j/trunk/slf4j-site/src/site/pages/extensions.html	Sat Nov 15 23:10:29 2008
@@ -296,6 +296,30 @@
 &lt;/configuration>  </p>
 
 
+<!-- .............................................................. -->
+
+   <h2><a name="mdcStrLookup"></a>MDCStrLookup</h2>
+
+   <p>StrLookup is a class defined in Apache Commons Lang. It is used
+   in conjunction with the StrSubstitutor class to allow Strings to
+   have tokens in the Strings dynamically replaced at run time. There
+   are many cases where it is desirable to merge the values for keys
+   in the SLF4J MDC into Strings. MDCStrLookup makes this possible.
+   </p>
+   <p>
+   Apache Commons Configuration provides a ConfigurationInterpolator
+   class. This class allows new StrLookups to be registered and the
+   values can then be used to merge with both the configuration of
+   Commons Configuration as well as the configuration files it manages.
+   </p>
+   <p>
+   StrLookup obviously has a dependency on Commons Lang. The Maven
+   pom.xml for slf4j-ext lists this dependency as optional so
+   that those wishing to use other extensions are not required to
+   unnecessarily package the commons lang jar. Therefore, when using
+   MDCStrLookup the dependency for commons-lang must be explicitly
+   declared along with slf4j-ext.    
+   </p>
 
 <!-- .............................................................. -->
 



More information about the slf4j-dev mailing list