You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ni...@apache.org on 2013/08/31 19:22:44 UTC
svn commit: r1519193 - in /logging/log4j/log4j2/trunk:
log4j-core/src/main/java/org/apache/logging/log4j/core/web/
log4j-core/src/test/java/org/apache/logging/log4j/core/web/ src/changes/
Author: nickwilliams
Date: Sat Aug 31 17:22:44 2013
New Revision: 1519193
URL: http://svn.apache.org/r1519193
Log:
[LOG4J2-359] Changed the Servlet 3.0 auto-initializer so that it does nothing in a Servlet 2.5 or older application. This ensures behavioral consistency across containers. Thanks to Abhinav Shah for the patch this change was based on.
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/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.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=1519193&r1=1519192&r2=1519193&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 Sat Aug 31 17:22:44 2013
@@ -23,24 +23,33 @@ import javax.servlet.FilterRegistration;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
+import javax.servlet.UnavailableException;
/**
* In a Servlet 3.0 or newer environment, this initializer is responsible for starting up Log4j logging before anything
- * else happens in application initialization.
+ * else happens in application initialization. For consistency across all containers, if the effective Servlet major
+ * version of the application is less than 3.0, this initializer does nothing.
*/
public class Log4jServletContainerInitializer implements ServletContainerInitializer {
@Override
public void onStartup(final Set<Class<?>> classes, final ServletContext servletContext) throws ServletException {
- servletContext.log("Log4jServletContainerInitializer starting up Log4j in Servlet 3.0+ environment.");
+ if (servletContext.getMajorVersion() > 2) {
+ servletContext.log("Log4jServletContainerInitializer starting up Log4j in Servlet 3.0+ environment.");
- final Log4jWebInitializer initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(servletContext);
- initializer.initialize();
- initializer.setLoggerContext(); // the application is just now starting to start up
+ final Log4jWebInitializer initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(servletContext);
+ initializer.initialize();
+ initializer.setLoggerContext(); // the application is just now starting to start up
- servletContext.addListener(new Log4jServletContextListener());
+ servletContext.addListener(new Log4jServletContextListener());
- final FilterRegistration.Dynamic filter = servletContext.addFilter("log4jServletFilter", new Log4jServletFilter());
- filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
+ final FilterRegistration.Dynamic filter =
+ servletContext.addFilter("log4jServletFilter", new Log4jServletFilter());
+ if (filter == null) {
+ throw new UnavailableException("In a Servlet 3.0+ application, you must not define a " +
+ "log4jServletFilter in web.xml. Log4j 2 defines this for you automatically.");
+ }
+ filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
+ }
}
}
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=1519193&r1=1519192&r2=1519193&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 Sat Aug 31 17:22:44 2013
@@ -30,8 +30,7 @@ import org.junit.Before;
import org.junit.Test;
import static org.easymock.EasyMock.*;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
public class Log4jServletContainerInitializerTest {
private ServletContext servletContext;
@@ -53,12 +52,22 @@ public class Log4jServletContainerInitia
}
@Test
- public void testOnStartup() throws Exception {
+ public void testOnStartupWithServletVersion2_x() throws Exception {
+ expect(this.servletContext.getMajorVersion()).andReturn(2);
+
+ replay(this.servletContext, this.initializer);
+
+ this.containerInitializer.onStartup(null, this.servletContext);
+ }
+
+ @Test
+ public void testOnStartupWithServletVersion3_x() throws Exception {
final FilterRegistration.Dynamic registration = createStrictMock(FilterRegistration.Dynamic.class);
final Capture<EventListener> listenerCapture = new Capture<EventListener>();
final Capture<Filter> filterCapture = new Capture<Filter>();
+ expect(this.servletContext.getMajorVersion()).andReturn(3);
this.servletContext.log(anyObject(String.class));
expectLastCall();
expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
@@ -80,9 +89,40 @@ public class Log4jServletContainerInitia
}
@Test
- public void testOnStartupFailed() throws Exception {
+ public void testOnStartupFailedDueToPreExistingFilter() throws Exception {
+ final Capture<EventListener> listenerCapture = new Capture<EventListener>();
+ final Capture<Filter> filterCapture = new Capture<Filter>();
+
+ expect(this.servletContext.getMajorVersion()).andReturn(3);
+ this.servletContext.log(anyObject(String.class));
+ expectLastCall();
+ expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
+ this.initializer.initialize();
+ expectLastCall();
+ this.initializer.setLoggerContext();
+ expectLastCall();
+ this.servletContext.addListener(capture(listenerCapture));
+ expectLastCall();
+ expect(this.servletContext.addFilter(eq("log4jServletFilter"), capture(filterCapture))).andReturn(null);
+
+ replay(this.servletContext, this.initializer);
+
+ try {
+ this.containerInitializer.onStartup(null, this.servletContext);
+ fail("Expected an UnavailableException, got no exception.");
+ } catch (final UnavailableException e) {
+ assertEquals("The exception is not correct.",
+ "In a Servlet 3.0+ application, you must not define a log4jServletFilter in web.xml. Log4j 2 " +
+ "defines this for you automatically.",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void testOnStartupFailedDueToInitializerFailure() throws Exception {
final UnavailableException exception = new UnavailableException("");
+ expect(this.servletContext.getMajorVersion()).andReturn(3);
this.servletContext.log(anyObject(String.class));
expectLastCall();
expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
@@ -93,7 +133,7 @@ public class Log4jServletContainerInitia
try {
this.containerInitializer.onStartup(null, this.servletContext);
- fail("");
+ fail("Expected the exception thrown by the initializer; got no exception.");
} catch (final UnavailableException e) {
assertSame("The exception is not correct.", exception, e);
}
Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1519193&r1=1519192&r2=1519193&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sat Aug 31 17:22:44 2013
@@ -21,6 +21,10 @@
</properties>
<body>
<release version="2.0-beta9" date="soon, very soon" description="Bug fixes and enhancements">
+ <action issue="LOG4J2-359" dev="nickwilliams" type="fix" due-to="Abhinav Shah">
+ Changed the Servlet 3.0 auto-initializer so that it does nothing in a Servlet 2.5 or older application. This
+ ensures behavioral consistency across containers.
+ </action>
<action dev="nickwilliams" type="update">
Cleaned up tests and cleared up documentation for the JPA appender following the resolution of EclipseLink
issue #412454.