logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1488095 - in /logging/log4j/log4j2/trunk: src/changes/ web/src/main/java/org/apache/logging/log4j/core/web/ web/src/site/xdoc/ web/src/test/java/org/apache/logging/log4j/core/web/
Date Fri, 31 May 2013 05:11:42 GMT
Author: rgoers
Date: Fri May 31 05:11:42 2013
New Revision: 1488095

URL: http://svn.apache.org/r1488095
Log:
LOG4J2-249 - Allow context parameters in Log4jContextListener to include properties.

Modified:
    logging/log4j/log4j2/trunk/src/changes/changes.xml
    logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/Log4jContextListener.java
    logging/log4j/log4j2/trunk/web/src/site/xdoc/index.xml
    logging/log4j/log4j2/trunk/web/src/test/java/org/apache/logging/log4j/core/web/Log4jContextListenerTest.java

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1488095&r1=1488094&r2=1488095&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Fri May 31 05:11:42 2013
@@ -22,6 +22,9 @@
   </properties>
   <body>
     <release version="2.0-beta7" date="2013-??-??" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-249" dev="rgoers" type="update">
+        Allow context parameters in Log4jContextListener to include properties.
+      </action>
       <action issue="LOG4J2-263" dev="rgoers" type="fix">
         Do not allow a charset on RFC5424Layout - use UTF-8.
       </action>

Modified: logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/Log4jContextListener.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/Log4jContextListener.java?rev=1488095&r1=1488094&r2=1488095&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/Log4jContextListener.java
(original)
+++ logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/Log4jContextListener.java
Fri May 31 05:11:42 2013
@@ -22,6 +22,9 @@ import javax.servlet.ServletContextListe
 
 import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.lookup.Interpolator;
+import org.apache.logging.log4j.core.lookup.StrLookup;
+import org.apache.logging.log4j.core.lookup.StrSubstitutor;
 
 /**
  * Saves the LoggerContext into the ServletContext as an attribute.
@@ -43,6 +46,8 @@ public class Log4jContextListener implem
      */
     public static final String LOG4J_CONTEXT_NAME = "log4jContextName";
 
+    private final StrSubstitutor subst = new StrSubstitutor(new Interpolator());
+
     /**
      * Initialize Logging for the web application.
      * @param event The ServletContextEvent.
@@ -50,8 +55,8 @@ public class Log4jContextListener implem
     @Override
     public void contextInitialized(final ServletContextEvent event) {
         final ServletContext context = event.getServletContext();
-        final String locn = context.getInitParameter(LOG4J_CONFIG);
-        String name = context.getInitParameter(LOG4J_CONTEXT_NAME);
+        final String locn = subst.replace(context.getInitParameter(LOG4J_CONFIG));
+        String name = subst.replace(context.getInitParameter(LOG4J_CONTEXT_NAME));
         if (name == null) {
             name = context.getServletContextName();
         }

Modified: logging/log4j/log4j2/trunk/web/src/site/xdoc/index.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/web/src/site/xdoc/index.xml?rev=1488095&r1=1488094&r2=1488095&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/web/src/site/xdoc/index.xml (original)
+++ logging/log4j/log4j2/trunk/web/src/site/xdoc/index.xml Fri May 31 05:11:42 2013
@@ -41,12 +41,16 @@
                   working directory of the running server.</li>
               </ol>
             </p>
-            <p>To register this listener, make sure that the log4j-web-2.0.jar file
is 
-            in the classpath (either in your web application's WEB-INF/lib directory 
+            <p>To register this listener, make sure that the log4j-web-2.0.jar file
is
+            in the classpath (either in your web application's WEB-INF/lib directory
             or in the container's classpath) and add the following snippet to your web.xml:</p>
             <pre>  &lt;listener&gt;
     &lt;listener-class>org.apache.logging.log4j.core.web.Log4jContextListener&lt;/listener-class&gt;
   &lt;/listener&gt;</pre>
+            <p>
+              Either or both of the initialization parameters may contain variables of the
form ${sys:propertyName} or
+              ${env:propertyName} to allow substitution using either a system property or
an environment variable.
+            </p>
           </subsection>
           <subsection name="JNDIContextFilter">
             <p>The JNDIContextFilter is a Servlet Filter that can be used in conjunction
with the JNDIContextSelector

Modified: logging/log4j/log4j2/trunk/web/src/test/java/org/apache/logging/log4j/core/web/Log4jContextListenerTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/web/src/test/java/org/apache/logging/log4j/core/web/Log4jContextListenerTest.java?rev=1488095&r1=1488094&r2=1488095&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/web/src/test/java/org/apache/logging/log4j/core/web/Log4jContextListenerTest.java
(original)
+++ logging/log4j/log4j2/trunk/web/src/test/java/org/apache/logging/log4j/core/web/Log4jContextListenerTest.java
Fri May 31 05:11:42 2013
@@ -69,6 +69,32 @@ public class Log4jContextListenerTest {
             config.getName(), NullConfiguration.NULL_NAME.equals(config.getName()));
     }
 
+
+    @Test
+    public void testFromProperty() throws Exception {
+        System.setProperty("targetDir", "target/test-classes");
+        final MockServletContext context = new MockServletContext();
+        context.setInitParameter(Log4jContextListener.LOG4J_CONTEXT_NAME, "Test1");
+        context.setInitParameter(Log4jContextListener.LOG4J_CONFIG, "${sys:targetDir}/log4j2-config.xml");
+        final Log4jContextListener listener = new Log4jContextListener();
+        final ServletContextEvent event = new ServletContextEvent(context);
+        listener.contextInitialized(event);
+        LogManager.getLogger("org.apache.test.TestConfigurator");
+        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+        Configuration config = ctx.getConfiguration();
+        assertNotNull("No configuration", config);
+        assertTrue("Incorrect Configuration. Expected " + CONFIG_NAME + " but found " + config.getName(),
+            CONFIG_NAME.equals(config.getName()));
+        final Map<String, Appender<?>> map = config.getAppenders();
+        assertTrue("No Appenders", map != null && map.size() > 0);
+        assertTrue("Wrong configuration", map.containsKey("List"));
+        listener.contextDestroyed(event);
+        config = ctx.getConfiguration();
+        assertTrue("Incorrect Configuration. Expected " + NullConfiguration.NULL_NAME + "
but found " +
+            config.getName(), NullConfiguration.NULL_NAME.equals(config.getName()));
+        System.clearProperty("targetDir");
+    }
+
     @Test
     public void testFromClassPath() throws Exception {
         final MockServletContext context = new MockServletContext();



Mime
View raw message