logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1517400 - in /logging/log4j/log4j2/trunk: core/ core/src/main/java/org/apache/logging/log4j/core/config/ core/src/main/java/org/apache/logging/log4j/core/lookup/ core/src/main/java/org/apache/logging/log4j/core/selector/ core/src/main/java...
Date Mon, 26 Aug 2013 00:08:03 GMT
Author: rgoers
Date: Mon Aug 26 00:08:03 2013
New Revision: 1517400

URL: http://svn.apache.org/r1517400
Log:
LOG4J2-364 - Add WebLookup

Added:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java
      - copied, changed from r1516894, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContextListenerTest.java
Modified:
    logging/log4j/log4j2/trunk/core/pom.xml
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/JNDIContextSelector.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/NamedContextSelector.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/core/pom.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/pom.xml?rev=1517400&r1=1517399&r2=1517400&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/pom.xml (original)
+++ logging/log4j/log4j2/trunk/core/pom.xml Mon Aug 26 00:08:03 2013
@@ -158,6 +158,12 @@
       <scope>compile</scope>
       <optional>true</optional>
     </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-test</artifactId>
+      <version>3.2.4.RELEASE</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <plugins>
@@ -286,7 +292,7 @@
           <detectOfflineLinks>false</detectOfflineLinks>
           <linksource>true</linksource>
           <links>
-            <link>http://docs.oracle.com/javaee/6/api/</link> 
+            <link>http://docs.oracle.com/javaee/6/api/</link>
           </links>
           <tags>
             <tag>

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java?rev=1517400&r1=1517399&r2=1517400&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
Mon Aug 26 00:08:03 2013
@@ -21,6 +21,7 @@ import java.net.URISyntaxException;
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.impl.ContextAnchor;
 import org.apache.logging.log4j.status.StatusLogger;
 
 /**
@@ -33,6 +34,7 @@ public final class Configurator {
     private Configurator() {
     }
 
+
     /**
      * Initializes the Logging Context.
      * @param name The Context name.
@@ -41,10 +43,24 @@ public final class Configurator {
      * @return The LoggerContext.
      */
     public static LoggerContext initialize(final String name, final ClassLoader loader, final
String configLocation) {
+        return initialize(name, loader, configLocation, null);
+
+    }
+
+    /**
+     * Initializes the Logging Context.
+     * @param name The Context name.
+     * @param loader The ClassLoader for the Context (or null).
+     * @param configLocation The configuration for the logging context.
+     * @param externalContext The external context to be attached to the LoggerContext
+     * @return The LoggerContext.
+     */
+    public static LoggerContext initialize(final String name, final ClassLoader loader, final
String configLocation,
+                                           final Object externalContext) {
 
         try {
             final URI uri = configLocation == null ? null : new URI(configLocation);
-            return initialize(name, loader, uri);
+            return initialize(name, loader, uri, externalContext);
         } catch (final URISyntaxException ex) {
             ex.printStackTrace();
         }
@@ -69,13 +85,31 @@ public final class Configurator {
      * @return The LoggerContext.
      */
     public static LoggerContext initialize(final String name, final ClassLoader loader, final
URI configLocation) {
+        return initialize(name, loader, configLocation, null);
+    }
+
+    /**
+     * Initializes the Logging Context.
+     * @param name The Context name.
+     * @param loader The ClassLoader for the Context (or null).
+     * @param configLocation The configuration for the logging context.
+     * @param externalContext The external context to be attached to the LoggerContext
+     * @return The LoggerContext.
+     */
+    public static LoggerContext initialize(final String name, final ClassLoader loader, final
URI configLocation,
+                                           final Object externalContext) {
 
         try {
             final org.apache.logging.log4j.spi.LoggerContext context = LogManager.getContext(loader,
false, configLocation);
             if (context instanceof LoggerContext) {
                 final LoggerContext ctx = (LoggerContext) context;
+                ContextAnchor.THREAD_CONTEXT.set(ctx);
+                if (externalContext != null) {
+                    ctx.setExternalContext(externalContext);
+                }
                 final Configuration config = ConfigurationFactory.getInstance().getConfiguration(name,
configLocation);
                 ctx.start(config);
+                ContextAnchor.THREAD_CONTEXT.remove();
                 return ctx;
             } else {
                 LOGGER.error("LogManager returned an instance of {} which does not implement
{}. Unable to initialize Log4j",
@@ -106,8 +140,10 @@ public final class Configurator {
             final org.apache.logging.log4j.spi.LoggerContext context = LogManager.getContext(loader,
false, configLocation);
             if (context instanceof LoggerContext) {
                 final LoggerContext ctx = (LoggerContext) context;
+                ContextAnchor.THREAD_CONTEXT.set(ctx);
                 final Configuration config = ConfigurationFactory.getInstance().getConfiguration(source);
                 ctx.start(config);
+                ContextAnchor.THREAD_CONTEXT.remove();
                 return ctx;
             } else {
                 LOGGER.error("LogManager returned an instance of {} which does not implement
{}. Unable to initialize Log4j",

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java?rev=1517400&r1=1517399&r2=1517400&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
Mon Aug 26 00:08:03 2013
@@ -64,6 +64,7 @@ public class Interpolator implements Str
         lookups.put("sys", new SystemPropertiesLookup());
         lookups.put("env", new EnvironmentLookup());
         lookups.put("jndi", new JndiLookup());
+        lookups.put("web", new WebLookup());
     }
 
      /**

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/JNDIContextSelector.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/JNDIContextSelector.java?rev=1517400&r1=1517399&r2=1517400&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/JNDIContextSelector.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/JNDIContextSelector.java
Mon Aug 26 00:08:03 2013
@@ -117,17 +117,17 @@ public class JNDIContextSelector impleme
             LOGGER.error("Unable to lookup " + Constants.JNDI_CONTEXT_NAME, ne);
         }
 
-        return loggingContextName == null ? CONTEXT : locateContext(loggingContextName, configLocation);
+        return loggingContextName == null ? CONTEXT : locateContext(loggingContextName, null,
configLocation);
     }
 
     @Override
-    public LoggerContext locateContext(final String name, final URI configLocation) {
+    public LoggerContext locateContext(final String name, final Object externalContext, final
URI configLocation) {
         if (name == null) {
             LOGGER.error("A context name is required to locate a LoggerContext");
             return null;
         }
         if (!CONTEXT_MAP.containsKey(name)) {
-            final LoggerContext ctx = new LoggerContext(name, null, configLocation);
+            final LoggerContext ctx = new LoggerContext(name, externalContext, configLocation);
             CONTEXT_MAP.putIfAbsent(name, ctx);
         }
         return CONTEXT_MAP.get(name);

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/NamedContextSelector.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/NamedContextSelector.java?rev=1517400&r1=1517399&r2=1517400&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/NamedContextSelector.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/NamedContextSelector.java
Mon Aug 26 00:08:03 2013
@@ -28,10 +28,11 @@ public interface NamedContextSelector ex
     /**
      * Locate the LoggerContext with the specified name.
      * @param name The LoggerContext name.
+     * @param externalContext The external context to associate with the LoggerContext.
      * @param configLocation The location of the configuration.
      * @return A LoggerContext.
      */
-    LoggerContext locateContext(String name, URI configLocation);
+    LoggerContext locateContext(String name, Object externalContext, URI configLocation);
 
     /**
      * Locate the LoggerContext with the specified name using the default configuration.

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java?rev=1517400&r1=1517399&r2=1517400&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
Mon Aug 26 00:08:03 2013
@@ -104,7 +104,12 @@ final class Log4jWebInitializerImpl impl
             final ContextSelector selector = ((Log4jContextFactory) factory).getSelector();
             if (selector instanceof NamedContextSelector) {
                 this.selector = (NamedContextSelector) selector;
-                loggerContext = this.selector.locateContext(this.name, configLocation);
+                loggerContext = this.selector.locateContext(this.name, this.servletContext,
configLocation);
+                ContextAnchor.THREAD_CONTEXT.set(loggerContext);
+                if (loggerContext.getStatus() == LoggerContext.Status.INITIALIZED) {
+                    loggerContext.start();
+                }
+                ContextAnchor.THREAD_CONTEXT.remove();
             } else {
                 this.servletContext.log("Potential problem: Selector is not an instance of
NamedContextSelector.");
                 return;
@@ -128,7 +133,7 @@ final class Log4jWebInitializerImpl impl
             return;
         }
 
-        this.loggerContext = Configurator.initialize(this.name, this.getClassLoader(), location);
+        this.loggerContext = Configurator.initialize(this.name, this.getClassLoader(), location,
this.servletContext);
     }
 
     @Override
@@ -147,6 +152,7 @@ final class Log4jWebInitializerImpl impl
                     this.selector.removeContext(this.name);
                 }
                 this.loggerContext.stop();
+                this.loggerContext.setExternalContext(null);
                 this.loggerContext = null;
             }
         }

Copied: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java
(from r1516894, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContextListenerTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java?p2=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java&p1=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContextListenerTest.java&r1=1516894&r2=1517400&rev=1517400&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContextListenerTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java
Mon Aug 26 00:08:03 2013
@@ -16,97 +16,55 @@
  */
 package org.apache.logging.log4j.core.web;
 
-import javax.servlet.ServletContext;
-import javax.servlet.ServletContextEvent;
-import javax.servlet.UnavailableException;
-
+import org.apache.logging.log4j.core.LoggerContext;
+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 static org.easymock.EasyMock.*;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
-
-public class Log4jServletContextListenerTest {
-    private ServletContextEvent event;
-    private ServletContext servletContext;
-    private Log4jWebInitializer initializer;
-
-    private Log4jServletContextListener listener;
-
-    @Before
-    public void setUp() {
-        this.event = createStrictMock(ServletContextEvent.class);
-        this.servletContext = createStrictMock(ServletContext.class);
-        this.initializer = createStrictMock(Log4jWebInitializer.class);
-
-        this.listener = new Log4jServletContextListener();
-    }
+import javax.servlet.ServletContext;
+import javax.servlet.UnavailableException;
 
-    @After
-    public void tearDown() {
-        verify(this.event, this.servletContext, this.initializer);
-    }
+import org.springframework.mock.web.MockServletContext;
 
-    @Test
-    public void testInitAndDestroy() throws Exception {
-        expect(this.event.getServletContext()).andReturn(this.servletContext);
-        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();
-
-        replay(this.event, this.servletContext, this.initializer);
-
-        this.listener.contextInitialized(this.event);
-
-        verify(this.event, this.servletContext, this.initializer);
-        reset(this.event, this.servletContext, this.initializer);
-
-        this.servletContext.log(anyObject(String.class));
-        expectLastCall();
-        this.initializer.clearLoggerContext();
-        expectLastCall();
-        this.initializer.deinitialize();
-        expectLastCall();
+import static org.junit.Assert.*;
 
-        replay(this.event, this.servletContext, this.initializer);
-
-        this.listener.contextDestroyed(this.event);
-    }
+public class WebLookupTest {
 
     @Test
-    public void testInitFailure() throws Exception {
-        expect(this.event.getServletContext()).andReturn(this.servletContext);
-        this.servletContext.log(anyObject(String.class));
-        expectLastCall();
-        expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
-        this.initializer.initialize();
-        expectLastCall().andThrow(new UnavailableException(""));
-
-        replay(this.event, this.servletContext, this.initializer);
-
+    public void testLookup() throws Exception {
+        ServletContext servletContext = new MockServletContext();
+        servletContext.setAttribute("TestAttr", "AttrValue");
+        servletContext.setInitParameter("TestParam", "ParamValue");
+        servletContext.setAttribute("Name1", "Ben");
+        servletContext.setInitParameter("Name2", "Jerry");
+        Log4jWebInitializer initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(servletContext);
         try {
-            this.listener.contextInitialized(this.event);
-            fail("Expected a RuntimeException.");
-        } catch (final RuntimeException e) {
-            assertEquals("The message is not correct.", "Failed to initialize Log4j properly.",
e.getMessage());
+            initializer.initialize();
+            initializer.setLoggerContext();
+            LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
+            assertNotNull("No LoggerContext", ctx);
+            Configuration config = ctx.getConfiguration();
+            assertNotNull("No Configuration", config);
+            StrSubstitutor substitutor = config.getStrSubstitutor();
+            assertNotNull("No Interpolator", substitutor);
+            String value = substitutor.replace("${web:initParam.TestParam}");
+            assertNotNull("No value for TestParam", value);
+            assertTrue("Incorrect value for TestParam: " + value, "ParamValue".equals(value));
+            value = substitutor.replace("${web:attr.TestAttr}");
+            assertNotNull("No value for TestAttr", value);
+            assertTrue("Incorrect value for TestAttr: " + value, "AttrValue".equals(value));
+            value = substitutor.replace("${web:Name1}");
+            assertNotNull("No value for Name1", value);
+            assertTrue("Incorrect value for Name1: " + value, "Ben".equals(value));
+            value = substitutor.replace("${web:Name2}");
+            assertNotNull("No value for Name2", value);
+            assertTrue("Incorrect value for Name2: " + value, "Jerry".equals(value));
+        } catch (final UnavailableException e) {
+            fail("Failed to initialize Log4j properly." + e.getMessage());
         }
     }
 
-    @Test
-    public void testDestroy() {
-        replay(this.event, this.servletContext, this.initializer);
-
-        try {
-            this.listener.contextDestroyed(this.event);
-            fail("Expected an IllegalStateException.");
-        } catch (final IllegalStateException ignore) {
-
-        }
-    }
 }

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1517400&r1=1517399&r2=1517400&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Mon Aug 26 00:08:03 2013
@@ -21,6 +21,9 @@
   </properties>
   <body>
     <release version="2.0-beta9" date="soon, very soon" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-364" dev="rgoers" type="add" due-to="David Nault">
+        Add WebLookup to retrieve information from the ServletContext.
+      </action>
       <action issue="LOG4J2-367" dev="ggregory" type="fix" due-to="David Parry">
         JMS appenders send two messages for one append.
       </action>



Mime
View raw message