Author: nickwilliams
Date: Tue Jan 28 07:13:26 2014
New Revision: 1561956
URL: http://svn.apache.org/r1561956
Log:
Fixed LOG4J2-512 (Part 1-Code): Exposed Log4j web support interface and methods
and the LoggerContext through ServletContext attributes so that threads not
affected by filters (such as asynchronous threads) can utilize the
LoggerContext. Also updated the Log4j filter so that it supports async. Will
update documentation in Part 2 commit.
Added:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebSupport.java
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializer.java
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContextListenerTest.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletFilterTest.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImplTest.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java
logging/log4j/log4j2/trunk/src/changes/changes.xml
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java?rev=1561956&r1=1561955&r2=1561956&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java
Tue Jan 28 07:13:26 2014
@@ -54,6 +54,8 @@ public class Log4jServletContainerInitia
initializer.setLoggerContext(); // the application is just now
starting to start up
servletContext.addListener(new Log4jServletContextListener());
+
+ filter.setAsyncSupported(true); // supporting async when the user
isn't using async has no downsides
filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false,
"/*");
}
}
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializer.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializer.java?rev=1561956&r1=1561955&r2=1561956&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializer.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializer.java
Tue Jan 28 07:13:26 2014
@@ -21,30 +21,10 @@ import javax.servlet.UnavailableExceptio
/**
* Specifies an interface for initializing and deinitializing Log4j in a Java
EE web application. The default and only
* implementation is {@link Log4jWebInitializerImpl}. The initializer is based
on an interface to improve testability.
+ * The methods here are contained in a package-private sub-interface because
general application code should not have
+ * access to them.
*/
-interface Log4jWebInitializer {
- /**
- * The {@link javax.servlet.ServletContext} context-param name for the
name of the
- * {@link org.apache.logging.log4j.core.LoggerContext}.
- */
- String LOG4J_CONTEXT_NAME = "log4jContextName";
-
- /**
- * The {@link javax.servlet.ServletContext} context-param name for the
location of the configuration.
- */
- String LOG4J_CONFIG_LOCATION = "log4jConfiguration";
-
- /**
- * The {@link javax.servlet.ServletContext} context-param name for the
JNDI flag.
- */
- String IS_LOG4J_CONTEXT_SELECTOR_NAMED = "isLog4jContextSelectorNamed";
-
- /**
- * The attribute key for the {@link javax.servlet.ServletContext}
attribute that the singleton initializer instance
- * is stored in.
- */
- String INITIALIZER_ATTRIBUTE = Log4jWebInitializer.class.getName() +
".INSTANCE";
-
+interface Log4jWebInitializer extends Log4jWebSupport {
/**
* Starts up Log4j in the web application. Calls {@link
#setLoggerContext()} after initialization is complete.
*
@@ -57,15 +37,4 @@ interface Log4jWebInitializer {
* begins.
*/
void deinitialize();
-
- /**
- * Sets the logger context so that code executing afterwards can easily
and quickly access loggers via
- * {@link org.apache.logging.log4j.LogManager#getLogger}.
- */
- void setLoggerContext();
-
- /**
- * Clears the logger context set up in {@link #setLoggerContext()}.
- */
- void clearLoggerContext();
}
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java?rev=1561956&r1=1561955&r2=1561956&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
Tue Jan 28 07:13:26 2014
@@ -87,6 +87,8 @@ final class Log4jWebInitializerImpl impl
} else {
this.initializeNonJndi(location);
}
+
+ this.servletContext.setAttribute(CONTEXT_ATTRIBUTE,
this.loggerContext);
}
}
@@ -154,6 +156,7 @@ final class Log4jWebInitializerImpl impl
if (this.loggerContext != null) {
this.servletContext.log("Removing LoggerContext for [" +
this.name + "].");
+ this.servletContext.removeAttribute(CONTEXT_ATTRIBUTE);
if (this.selector != null) {
this.selector.removeContext(this.name);
}
@@ -197,10 +200,10 @@ final class Log4jWebInitializerImpl impl
*/
static Log4jWebInitializer getLog4jWebInitializer(final ServletContext
servletContext) {
synchronized (MUTEX) {
- Log4jWebInitializer initializer = (Log4jWebInitializer)
servletContext.getAttribute(INITIALIZER_ATTRIBUTE);
+ Log4jWebInitializer initializer = (Log4jWebInitializer)
servletContext.getAttribute(SUPPORT_ATTRIBUTE);
if (initializer == null) {
initializer = new Log4jWebInitializerImpl(servletContext);
- servletContext.setAttribute(INITIALIZER_ATTRIBUTE,
initializer);
+ servletContext.setAttribute(SUPPORT_ATTRIBUTE, initializer);
}
return initializer;
}
Added:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebSupport.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebSupport.java?rev=1561956&view=auto
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebSupport.java
(added)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebSupport.java
Tue Jan 28 07:13:26 2014
@@ -0,0 +1,55 @@
+package org.apache.logging.log4j.core.web;
+
+import org.apache.logging.log4j.spi.LoggerContext;
+
+/**
+ * Specifies an interface for setting and clearing a thread-bound {@link
LoggerContext} in a Java EE web application.
+ * Also defines constants for context parameter and attribute names. In most
cases you will never need to use this
+ * directly because the Log4j filter handles this task automatically. However,
in async operations you should wrap
+ * code that executes in separate threads with {@link #setLoggerContext} and
{@link #clearLoggerContext}.<br>
+ * <br>
+ * You can obtain the instance of this for your web application by retrieving
the {@link javax.servlet.ServletContext}
+ * attribute named {@code
org.apache.logging.log4j.core.web.Log4jWebSupport.INSTANCE}. If needed, you can
also obtain
+ * the {@link LoggerContext} instance for your web application by retrieving
the {@code ServletContext} attribute named
+ * {@code org.apache.logging.log4j.spi.LoggerContext.INSTANCE}.
+ */
+public interface Log4jWebSupport {
+ /**
+ * The {@link javax.servlet.ServletContext} context-param name for the
name of the
+ * {@link org.apache.logging.log4j.core.LoggerContext}.
+ */
+ String LOG4J_CONTEXT_NAME = "log4jContextName";
+
+ /**
+ * The {@link javax.servlet.ServletContext} context-param name for the
location of the configuration.
+ */
+ String LOG4J_CONFIG_LOCATION = "log4jConfiguration";
+
+ /**
+ * The {@link javax.servlet.ServletContext} context-param name for the
JNDI flag.
+ */
+ String IS_LOG4J_CONTEXT_SELECTOR_NAMED = "isLog4jContextSelectorNamed";
+
+ /**
+ * The attribute key for the {@link javax.servlet.ServletContext}
attribute that the singleton support instance
+ * is stored in.
+ */
+ String SUPPORT_ATTRIBUTE = Log4jWebSupport.class.getName() + ".INSTANCE";
+
+ /**
+ * The attribute key for the {@link javax.servlet.ServletContext}
attribute that the {@link LoggerContext}
+ * is stored in.
+ */
+ String CONTEXT_ATTRIBUTE = LoggerContext.class.getName() + ".INSTANCE";
+
+ /**
+ * Sets the logger context so that code executing afterwards can easily
and quickly access loggers via
+ * {@link org.apache.logging.log4j.LogManager#getLogger}.
+ */
+ void setLoggerContext();
+
+ /**
+ * Clears the logger context set up in {@link #setLoggerContext()}.
+ */
+ void clearLoggerContext();
+}
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java?rev=1561956&r1=1561955&r2=1561956&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java
Tue Jan 28 07:13:26 2014
@@ -82,13 +82,15 @@ public class Log4jServletContainerInitia
this.servletContext.log(anyObject(String.class));
expectLastCall();
expect(this.servletContext.addFilter(eq("log4jServletFilter"),
capture(filterCapture))).andReturn(registration);
-
expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
+
expect(this.servletContext.getAttribute(Log4jWebSupport.SUPPORT_ATTRIBUTE)).andReturn(this.initializer);
this.initializer.initialize();
expectLastCall();
this.initializer.setLoggerContext();
expectLastCall();
this.servletContext.addListener(capture(listenerCapture));
expectLastCall();
+ registration.setAsyncSupported(true);
+ expectLastCall();
registration.addMappingForUrlPatterns(eq(EnumSet.allOf(DispatcherType.class)),
eq(false), eq("/*"));
expectLastCall();
@@ -142,7 +144,7 @@ public class Log4jServletContainerInitia
this.servletContext.log(anyObject(String.class));
expectLastCall();
expect(this.servletContext.addFilter(eq("log4jServletFilter"),
capture(filterCapture))).andReturn(registration);
-
expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
+
expect(this.servletContext.getAttribute(Log4jWebSupport.SUPPORT_ATTRIBUTE)).andReturn(this.initializer);
this.initializer.initialize();
expectLastCall().andThrow(exception);
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContextListenerTest.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContextListenerTest.java?rev=1561956&r1=1561955&r2=1561956&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContextListenerTest.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContextListenerTest.java
Tue Jan 28 07:13:26 2014
@@ -54,7 +54,7 @@ public class Log4jServletContextListener
expect(this.event.getServletContext()).andReturn(this.servletContext);
this.servletContext.log(anyObject(String.class));
expectLastCall();
-
expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
+
expect(this.servletContext.getAttribute(Log4jWebSupport.SUPPORT_ATTRIBUTE)).andReturn(this.initializer);
this.initializer.initialize();
expectLastCall();
this.initializer.setLoggerContext();
@@ -84,7 +84,7 @@ public class Log4jServletContextListener
expect(this.event.getServletContext()).andReturn(this.servletContext);
this.servletContext.log(anyObject(String.class));
expectLastCall();
-
expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
+
expect(this.servletContext.getAttribute(Log4jWebSupport.SUPPORT_ATTRIBUTE)).andReturn(this.initializer);
this.initializer.initialize();
expectLastCall().andThrow(new UnavailableException(""));
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletFilterTest.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletFilterTest.java?rev=1561956&r1=1561955&r2=1561956&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletFilterTest.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletFilterTest.java
Tue Jan 28 07:13:26 2014
@@ -54,7 +54,7 @@ public class Log4jServletFilterTest {
expect(this.filterConfig.getServletContext()).andReturn(this.servletContext);
this.servletContext.log(anyObject(String.class));
expectLastCall();
-
expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
+
expect(this.servletContext.getAttribute(Log4jWebSupport.SUPPORT_ATTRIBUTE)).andReturn(this.initializer);
this.initializer.clearLoggerContext();
expectLastCall();
@@ -87,7 +87,7 @@ public class Log4jServletFilterTest {
expect(this.filterConfig.getServletContext()).andReturn(this.servletContext);
this.servletContext.log(anyObject(String.class));
expectLastCall();
-
expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
+
expect(this.servletContext.getAttribute(Log4jWebSupport.SUPPORT_ATTRIBUTE)).andReturn(this.initializer);
this.initializer.clearLoggerContext();
expectLastCall();
@@ -124,7 +124,7 @@ public class Log4jServletFilterTest {
expect(this.filterConfig.getServletContext()).andReturn(this.servletContext);
this.servletContext.log(anyObject(String.class));
expectLastCall();
-
expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
+
expect(this.servletContext.getAttribute(Log4jWebSupport.SUPPORT_ATTRIBUTE)).andReturn(this.initializer);
this.initializer.clearLoggerContext();
expectLastCall();
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImplTest.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImplTest.java?rev=1561956&r1=1561955&r2=1561956&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImplTest.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImplTest.java
Tue Jan 28 07:13:26 2014
@@ -39,8 +39,8 @@ public class Log4jWebInitializerImplTest
final Capture<Log4jWebInitializer> initializerCapture = new
Capture<Log4jWebInitializer>();
this.servletContext = createStrictMock(ServletContext.class);
-
expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(null);
-
this.servletContext.setAttribute(eq(Log4jWebInitializer.INITIALIZER_ATTRIBUTE),
capture(initializerCapture));
+
expect(this.servletContext.getAttribute(Log4jWebSupport.SUPPORT_ATTRIBUTE)).andReturn(null);
+
this.servletContext.setAttribute(eq(Log4jWebSupport.SUPPORT_ATTRIBUTE),
capture(initializerCapture));
expectLastCall();
replay(this.servletContext);
@@ -97,11 +97,15 @@ public class Log4jWebInitializerImplTest
@Test
public void
testInitializeWithNoParametersThenSetLoggerContextThenDeinitialize() throws
Exception {
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONTEXT_NAME)).andReturn(null);
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONFIG_LOCATION)).andReturn(null);
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
+ Capture<Object> loggerContextCapture = new Capture<Object>();
+
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONTEXT_NAME)).andReturn(null);
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION)).andReturn(null);
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn(null);
expect(this.servletContext.getServletContextName()).andReturn("helloWorld01");
+
this.servletContext.setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE),
capture(loggerContextCapture));
+ expectLastCall();
replay(this.servletContext);
@@ -109,6 +113,12 @@ public class Log4jWebInitializerImplTest
this.initializer.initialize();
+ assertNotNull("The context attribute should not be null.",
loggerContextCapture.getValue());
+ assertTrue("The context attribute is not correct.",
+ loggerContextCapture.getValue() instanceof
org.apache.logging.log4j.spi.LoggerContext);
+ org.apache.logging.log4j.spi.LoggerContext loggerContext =
+
(org.apache.logging.log4j.spi.LoggerContext)loggerContextCapture.getValue();
+
verify(this.servletContext);
reset(this.servletContext);
replay(this.servletContext);
@@ -119,6 +129,7 @@ public class Log4jWebInitializerImplTest
final LoggerContext context = ContextAnchor.THREAD_CONTEXT.get();
assertNotNull("The context should not be null.", context);
+ assertSame("The context is not correct.", loggerContext, context);
this.initializer.clearLoggerContext();
@@ -129,6 +140,8 @@ public class Log4jWebInitializerImplTest
this.servletContext.log(anyObject(String.class));
expectLastCall();
+ this.servletContext.removeAttribute(Log4jWebSupport.CONTEXT_ATTRIBUTE);
+ expectLastCall();
replay(this.servletContext);
@@ -147,12 +160,16 @@ public class Log4jWebInitializerImplTest
@Test
public void
testInitializeWithClassLoaderNoParametersThenSetLoggerContextThenDeinitialize()
throws Exception {
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONTEXT_NAME)).andReturn(null);
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONFIG_LOCATION)).andReturn(null);
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
+ Capture<Object> loggerContextCapture = new Capture<Object>();
+
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONTEXT_NAME)).andReturn(null);
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION)).andReturn(null);
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn("false");
expect(this.servletContext.getServletContextName()).andReturn("helloWorld02");
expect(this.servletContext.getClassLoader()).andReturn(this.getClass().getClassLoader());
+
this.servletContext.setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE),
capture(loggerContextCapture));
+ expectLastCall();
replay(this.servletContext);
@@ -160,6 +177,12 @@ public class Log4jWebInitializerImplTest
this.initializer.initialize();
+ assertNotNull("The context attribute should not be null.",
loggerContextCapture.getValue());
+ assertTrue("The context attribute is not correct.",
+ loggerContextCapture.getValue() instanceof
org.apache.logging.log4j.spi.LoggerContext);
+ org.apache.logging.log4j.spi.LoggerContext loggerContext =
+
(org.apache.logging.log4j.spi.LoggerContext)loggerContextCapture.getValue();
+
verify(this.servletContext);
reset(this.servletContext);
replay(this.servletContext);
@@ -170,6 +193,7 @@ public class Log4jWebInitializerImplTest
final LoggerContext context = ContextAnchor.THREAD_CONTEXT.get();
assertNotNull("The context should not be null.", context);
+ assertSame("The context is not correct.", loggerContext, context);
this.initializer.clearLoggerContext();
@@ -180,6 +204,8 @@ public class Log4jWebInitializerImplTest
this.servletContext.log(anyObject(String.class));
expectLastCall();
+ this.servletContext.removeAttribute(Log4jWebSupport.CONTEXT_ATTRIBUTE);
+ expectLastCall();
replay(this.servletContext);
@@ -198,12 +224,16 @@ public class Log4jWebInitializerImplTest
@Test
public void testInitializeIsIdempotent() throws Exception {
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONTEXT_NAME)).andReturn(null);
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONFIG_LOCATION)).andReturn(null);
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
+ Capture<Object> loggerContextCapture = new Capture<Object>();
+
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONTEXT_NAME)).andReturn(null);
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION)).andReturn(null);
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn("nothing");
expect(this.servletContext.getServletContextName()).andReturn("helloWorld03");
expect(this.servletContext.getClassLoader()).andReturn(this.getClass().getClassLoader());
+
this.servletContext.setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE),
capture(loggerContextCapture));
+ expectLastCall();
replay(this.servletContext);
@@ -211,6 +241,10 @@ public class Log4jWebInitializerImplTest
this.initializer.initialize();
+ assertNotNull("The context attribute should not be null.",
loggerContextCapture.getValue());
+ assertTrue("The context attribute is not correct.",
+ loggerContextCapture.getValue() instanceof
org.apache.logging.log4j.spi.LoggerContext);
+
verify(this.servletContext);
reset(this.servletContext);
replay(this.servletContext);
@@ -224,6 +258,8 @@ public class Log4jWebInitializerImplTest
this.servletContext.log(anyObject(String.class));
expectLastCall();
+ this.servletContext.removeAttribute(Log4jWebSupport.CONTEXT_ATTRIBUTE);
+ expectLastCall();
replay(this.servletContext);
@@ -232,12 +268,16 @@ public class Log4jWebInitializerImplTest
@Test
public void testInitializeFailsAfterDeinitialize() throws Exception {
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONTEXT_NAME)).andReturn(null);
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONFIG_LOCATION)).andReturn(null);
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
+ Capture<Object> loggerContextCapture = new Capture<Object>();
+
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONTEXT_NAME)).andReturn(null);
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION)).andReturn(null);
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn(null);
expect(this.servletContext.getServletContextName()).andReturn("helloWorld04");
expect(this.servletContext.getClassLoader()).andReturn(this.getClass().getClassLoader());
+
this.servletContext.setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE),
capture(loggerContextCapture));
+ expectLastCall();
replay(this.servletContext);
@@ -245,11 +285,17 @@ public class Log4jWebInitializerImplTest
this.initializer.initialize();
+ assertNotNull("The context attribute should not be null.",
loggerContextCapture.getValue());
+ assertTrue("The context attribute is not correct.",
+ loggerContextCapture.getValue() instanceof
org.apache.logging.log4j.spi.LoggerContext);
+
verify(this.servletContext);
reset(this.servletContext);
this.servletContext.log(anyObject(String.class));
expectLastCall();
+ this.servletContext.removeAttribute(Log4jWebSupport.CONTEXT_ATTRIBUTE);
+ expectLastCall();
replay(this.servletContext);
@@ -265,12 +311,16 @@ public class Log4jWebInitializerImplTest
@Test
public void testDeinitializeIsIdempotent() throws Exception {
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONTEXT_NAME)).andReturn(null);
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONFIG_LOCATION)).andReturn(null);
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
+ Capture<Object> loggerContextCapture = new Capture<Object>();
+
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONTEXT_NAME)).andReturn(null);
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION)).andReturn(null);
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn(null);
expect(this.servletContext.getServletContextName()).andReturn("helloWorld05");
expect(this.servletContext.getClassLoader()).andReturn(this.getClass().getClassLoader());
+
this.servletContext.setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE),
capture(loggerContextCapture));
+ expectLastCall();
replay(this.servletContext);
@@ -278,11 +328,17 @@ public class Log4jWebInitializerImplTest
this.initializer.initialize();
+ assertNotNull("The context attribute should not be null.",
loggerContextCapture.getValue());
+ assertTrue("The context attribute is not correct.",
+ loggerContextCapture.getValue() instanceof
org.apache.logging.log4j.spi.LoggerContext);
+
verify(this.servletContext);
reset(this.servletContext);
this.servletContext.log(anyObject(String.class));
expectLastCall();
+ this.servletContext.removeAttribute(Log4jWebSupport.CONTEXT_ATTRIBUTE);
+ expectLastCall();
replay(this.servletContext);
@@ -293,9 +349,9 @@ public class Log4jWebInitializerImplTest
@Test
public void testInitializeUsingJndiSelectorFails() throws Exception {
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONTEXT_NAME)).andReturn(null);
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONFIG_LOCATION)).andReturn(null);
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONTEXT_NAME)).andReturn(null);
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION)).andReturn(null);
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn("true");
replay(this.servletContext);
@@ -312,12 +368,16 @@ public class Log4jWebInitializerImplTest
@Test
public void testInitializeUsingJndiSelector() throws Exception {
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONTEXT_NAME)).andReturn("helloWorld6");
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONFIG_LOCATION)).andReturn(null);
-
expect(this.servletContext.getInitParameter(Log4jWebInitializer.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
+ Capture<Object> loggerContextCapture = new Capture<Object>();
+
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONTEXT_NAME)).andReturn("helloWorld6");
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION)).andReturn(null);
+
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn("true");
this.servletContext.log(anyObject(String.class));
expectLastCall();
+
this.servletContext.setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE),
capture(loggerContextCapture));
+ expectLastCall();
replay(this.servletContext);
@@ -325,6 +385,8 @@ public class Log4jWebInitializerImplTest
this.initializer.initialize();
+ assertNull("The context attribute should be null.",
loggerContextCapture.getValue());
+
verify(this.servletContext);
reset(this.servletContext);
replay(this.servletContext);
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java?rev=1561956&r1=1561955&r2=1561956&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java
Tue Jan 28 07:13:26 2014
@@ -22,8 +22,6 @@ import org.apache.logging.log4j.core.app
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.impl.ContextAnchor;
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
-import org.junit.After;
-import org.junit.Before;
import org.junit.Test;
import javax.servlet.ServletContext;
Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1561956&r1=1561955&r2=1561956&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Tue Jan 28 07:13:26 2014
@@ -21,6 +21,11 @@
</properties>
<body>
<release version="2.0-RC1" date="2014-MM-DD" description="Bug fixes and
enhancements">
+ <action issue="LOG4J2-512" dev="nickwilliams" type="fix" due-to="Chandra
Sekhar Kakarla, Matt Sicker">
+ Exposed Log4j web support interface and methods and the LoggerContext
through ServletContext attributes
+ so that threads not affected by filters (such as asynchronous threads)
can utilize the LoggerContext. Also
+ updated the Log4j filter so that it supports async.
+ </action>
<action issue="LOG4J2-409" dev="nickwilliams" type="fix" due-to="Frank
Steinmann, Thomas Neidhart">
Created a utility to properly escape backslashes before creating URIs,
and changed URI creation to use the
utility instead of instantiating URI directly.