You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2014/08/19 03:46:18 UTC

svn commit: r1618779 - in /logging/log4j/log4j2/trunk/log4j-core/src/test: java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.java resources/ContextMapLookupTest.xml

Author: mattsicker
Date: Tue Aug 19 01:46:18 2014
New Revision: 1618779

URL: http://svn.apache.org/r1618779
Log:
Add test demo for LOG4J2-786.

Added:
    logging/log4j/log4j2/trunk/log4j-core/src/test/resources/ContextMapLookupTest.xml
Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.java

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.java?rev=1618779&r1=1618778&r2=1618779&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.java Tue Aug 19 01:46:18 2014
@@ -16,8 +16,18 @@
  */
 package org.apache.logging.log4j.core.lookup;
 
+import java.io.File;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.junit.InitialLoggerContext;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
 
 import static org.junit.Assert.*;
 
@@ -29,6 +39,33 @@ public class ContextMapLookupTest {
     private static final String TESTKEY = "TestKey";
     private static final String TESTVAL = "TestValue";
 
+    private InitialLoggerContext context = new InitialLoggerContext("ContextMapLookupTest.xml");
+
+    @Rule
+    public RuleChain chain = RuleChain.outerRule(new TestRule() {
+        @Override
+        public Statement apply(final Statement base, final Description description) {
+            return new Statement() {
+                @Override
+                public void evaluate() throws Throwable {
+                    final File logFile = new File("target",
+                        description.getClassName() + '.' + description.getMethodName() + ".log");
+                    ThreadContext.put("testClassName", description.getClassName());
+                    ThreadContext.put("testMethodName", description.getMethodName());
+                    try {
+                        base.evaluate();
+                    } finally {
+                        ThreadContext.remove("testClassName");
+                        ThreadContext.remove("testMethodName");
+                        if (logFile.exists()) {
+                            logFile.deleteOnExit();
+                        }
+                    }
+                }
+            };
+        }
+    }).around(context);
+
     @Test
     public void testLookup() {
         ThreadContext.put(TESTKEY, TESTVAL);
@@ -38,4 +75,18 @@ public class ContextMapLookupTest {
         value = lookup.lookup("BadKey");
         assertNull(value);
     }
+
+    /**
+     * Demonstrates the use of ThreadContext in determining what log file name to use in a unit test.
+     * Inspired by LOG4J2-786. Note that this only works because the ThreadContext is prepared
+     * <em>before</em> the Logger instance is obtained. This use of ThreadContext and the associated
+     * ContextMapLookup can be used in many other ways in a config file.
+     */
+    @Test
+    public void testFileLog() throws Exception {
+        final Logger logger = LogManager.getLogger();
+        logger.info("Hello from testFileLog!");
+        final File logFile = new File("target", this.getClass().getName() + ".testFileLog.log");
+        assertTrue(logFile.exists());
+    }
 }

Added: logging/log4j/log4j2/trunk/log4j-core/src/test/resources/ContextMapLookupTest.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/resources/ContextMapLookupTest.xml?rev=1618779&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/resources/ContextMapLookupTest.xml (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/resources/ContextMapLookupTest.xml Tue Aug 19 01:46:18 2014
@@ -0,0 +1,29 @@
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one or more
+  ~ contributor license agreements. See the NOTICE file distributed with
+  ~ this work for additional information regarding copyright ownership.
+  ~ The ASF licenses this file to You under the Apache license, Version 2.0
+  ~ (the "License"); you may not use this file except in compliance with
+  ~ the License. You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the license for the specific language governing permissions and
+  ~ limitations under the license.
+  -->
+
+<Configuration status="debug" name="ContextMapLookupTest">
+  <Appenders>
+    <File name="File" fileName="target/${ctx:testClassName}.${ctx:testMethodName}.log">
+      <PatternLayout pattern="%m%n"/>
+    </File>
+  </Appenders>
+  <Loggers>
+    <Root level="info">
+      <AppenderRef ref="File"/>
+    </Root>
+  </Loggers>
+</Configuration>
\ No newline at end of file