Author: nickwilliams
Date: Tue Jan 28 07:30:20 2014
New Revision: 1561958

URL: http://svn.apache.org/r1561958
Log:
Fixed LOG4J2-452 (Part 1-Code) Added a ServletContext attribute that, when set 
to 'true', disables Log4j's auto-initialization in Servlet 3.0+ web 
applications. Will update documentation in Part 2 commit.

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/Log4jWebSupport.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=1561958&r1=1561957&r2=1561958&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:30:20 2014
@@ -37,7 +37,10 @@ public class Log4jServletContainerInitia
 
     @Override
     public void onStartup(final Set<Class<?>> classes, final ServletContext 
servletContext) throws ServletException {
-        if (servletContext.getMajorVersion() > 2 && 
servletContext.getEffectiveMajorVersion() > 2) {
+        if (servletContext.getMajorVersion() > 2 && 
servletContext.getEffectiveMajorVersion() > 2 &&
+                !"true".equalsIgnoreCase(servletContext.getInitParameter(
+                        Log4jWebSupport.IS_LOG4J_AUTO_INITIALIZATION_DISABLED
+                ))) {
             servletContext.log("Log4jServletContainerInitializer starting up 
Log4j in Servlet 3.0+ environment.");
 
             final FilterRegistration.Dynamic filter =

Modified: 
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=1561958&r1=1561957&r2=1561958&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebSupport.java
 (original)
+++ 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebSupport.java
 Tue Jan 28 07:30:20 2014
@@ -15,22 +15,29 @@ import org.apache.logging.log4j.spi.Logg
  */
 public interface Log4jWebSupport {
     /**
-     * The {@link javax.servlet.ServletContext} context-param name for the 
name of the
+     * The {@link javax.servlet.ServletContext} parameter 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.
+     * The {@link javax.servlet.ServletContext} parameter name for the 
location of the configuration.
      */
     String LOG4J_CONFIG_LOCATION = "log4jConfiguration";
 
     /**
-     * The {@link javax.servlet.ServletContext} context-param name for the 
JNDI flag.
+     * The {@link javax.servlet.ServletContext} parameter name for the JNDI 
flag.
      */
     String IS_LOG4J_CONTEXT_SELECTOR_NAMED = "isLog4jContextSelectorNamed";
 
     /**
+     * The {@link javax.servlet.ServletContext} parameter name for the flag 
that disables Log4j's auto-initialization
+     * in Servlet 3.0+ web applications. Set a context parameter with this 
name to "true" to disable
+     * auto-initialization.
+     */
+    String IS_LOG4J_AUTO_INITIALIZATION_DISABLED = 
"isLog4jAutoInitializationDisabled";
+
+    /**
      * The attribute key for the {@link javax.servlet.ServletContext} 
attribute that the singleton support instance
      * is stored in.
      */

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=1561958&r1=1561957&r2=1561958&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:30:20 2014
@@ -71,6 +71,30 @@ public class Log4jServletContainerInitia
     }
 
     @Test
+    public void 
testOnStartupWithServletVersion3_xEffectiveVersion3_xDisabledTrue() throws 
Exception {
+        expect(this.servletContext.getMajorVersion()).andReturn(3);
+        expect(this.servletContext.getEffectiveMajorVersion()).andReturn(3);
+        
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_AUTO_INITIALIZATION_DISABLED))
+                .andReturn("true");
+
+        replay(this.servletContext, this.initializer);
+
+        this.containerInitializer.onStartup(null, this.servletContext);
+    }
+
+    @Test
+    public void 
testOnStartupWithServletVersion3_xEffectiveVersion3_xDisabledTRUE() throws 
Exception {
+        expect(this.servletContext.getMajorVersion()).andReturn(3);
+        expect(this.servletContext.getEffectiveMajorVersion()).andReturn(3);
+        
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_AUTO_INITIALIZATION_DISABLED))
+                .andReturn("TRUE");
+
+        replay(this.servletContext, this.initializer);
+
+        this.containerInitializer.onStartup(null, this.servletContext);
+    }
+
+    @Test
     public void testOnStartupWithServletVersion3_xEffectiveVersion3_x() throws 
Exception {
         final FilterRegistration.Dynamic registration = 
createStrictMock(FilterRegistration.Dynamic.class);
 
@@ -79,6 +103,8 @@ public class Log4jServletContainerInitia
 
         expect(this.servletContext.getMajorVersion()).andReturn(3);
         expect(this.servletContext.getEffectiveMajorVersion()).andReturn(3);
+        
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_AUTO_INITIALIZATION_DISABLED))
+                .andReturn(null);
         this.servletContext.log(anyObject(String.class));
         expectLastCall();
         expect(this.servletContext.addFilter(eq("log4jServletFilter"), 
capture(filterCapture))).andReturn(registration);
@@ -115,6 +141,8 @@ public class Log4jServletContainerInitia
 
         expect(this.servletContext.getMajorVersion()).andReturn(3);
         expect(this.servletContext.getEffectiveMajorVersion()).andReturn(3);
+        
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_AUTO_INITIALIZATION_DISABLED))
+                .andReturn("false");
         this.servletContext.log(anyObject(String.class));
         expectLastCall();
         expect(this.servletContext.addFilter(eq("log4jServletFilter"), 
capture(filterCapture))).andReturn(null);
@@ -141,6 +169,8 @@ public class Log4jServletContainerInitia
 
         expect(this.servletContext.getMajorVersion()).andReturn(3);
         expect(this.servletContext.getEffectiveMajorVersion()).andReturn(3);
+        
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_AUTO_INITIALIZATION_DISABLED))
+                .andReturn("balderdash");
         this.servletContext.log(anyObject(String.class));
         expectLastCall();
         expect(this.servletContext.addFilter(eq("log4jServletFilter"), 
capture(filterCapture))).andReturn(registration);

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1561958&r1=1561957&r2=1561958&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Tue Jan 28 07:30:20 2014
@@ -21,6 +21,10 @@
   </properties>
   <body>
     <release version="2.0-RC1" date="2014-MM-DD" description="Bug fixes and 
enhancements">
+      <action issue="LOG4J2-452" dev="nickwilliams" type="fix" due-to="">
+        Added a ServletContext attribute that, when set to "true", disables 
Log4j's auto-initialization in
+        Servlet 3.0+ web applications.
+      </action>
       <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


Reply via email to