You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2018/07/03 04:40:29 UTC

[1/3] logging-log4j2 git commit: [LOG4J2-1721] Allow composite configuration for context parameter

Repository: logging-log4j2
Updated Branches:
  refs/heads/release-2.x 76a7b1824 -> 5aef5b7db


[LOG4J2-1721] Allow composite configuration for context parameter

For non-JNDI, allow comma-separated configuration locations (composite
configuration syntax) for "log4jConfiguration" context parameter.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/e592d467
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/e592d467
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/e592d467

Branch: refs/heads/release-2.x
Commit: e592d4674f6d0e868fb4a3e7742f42cf223c5aa7
Parents: d48b1d9
Author: Phokham Nonava <pn...@nonava.com>
Authored: Wed Jun 6 16:32:21 2018 +0200
Committer: Phokham Nonava <pn...@nonava.com>
Committed: Thu Jun 7 17:14:38 2018 +0200

----------------------------------------------------------------------
 .../log4j/web/Log4jWebInitializerImpl.java      | 20 +++-
 .../log4j/web/Log4jWebInitializerImplTest.java  | 97 +++++++++++++++++++-
 2 files changed, 113 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e592d467/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java
----------------------------------------------------------------------
diff --git a/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java b/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java
index 6efa69a..ce36076 100644
--- a/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java
+++ b/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java
@@ -19,8 +19,10 @@ package org.apache.logging.log4j.web;
 import java.net.URI;
 import java.net.URL;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
@@ -158,16 +160,32 @@ final class Log4jWebInitializerImpl extends AbstractLifeCycle implements Log4jWe
             this.name = this.servletContext.getContextPath();
             LOGGER.debug("Using the servlet context context-path \"{}\".", this.name);
         }
-
         if (this.name == null && location == null) {
             LOGGER.error("No Log4j context configuration provided. This is very unusual.");
             this.name = new SimpleDateFormat("yyyyMMdd_HHmmss.SSS").format(new Date());
         }
+        if (location != null && location.contains(",")) {
+            final List<URI> uris = getConfigURIs(location);
+            this.loggerContext = Configurator.initialize(this.name, this.getClassLoader(), uris, this.servletContext);
+            return;
+        }
 
         final URI uri = getConfigURI(location);
         this.loggerContext = Configurator.initialize(this.name, this.getClassLoader(), uri, this.servletContext);
     }
 
+    private List<URI> getConfigURIs(final String location) {
+        final String[] parts = location.split(",");
+        final List<URI> uris = new ArrayList<>(parts.length);
+        for (final String part : parts) {
+            final URI uri = getConfigURI(part);
+            if (uri != null) {
+                uris.add(uri);
+            }
+        }
+        return uris;
+    }
+
     private URI getConfigURI(final String location) {
         try {
             String configLocation = location;

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e592d467/log4j-web/src/test/java/org/apache/logging/log4j/web/Log4jWebInitializerImplTest.java
----------------------------------------------------------------------
diff --git a/log4j-web/src/test/java/org/apache/logging/log4j/web/Log4jWebInitializerImplTest.java b/log4j-web/src/test/java/org/apache/logging/log4j/web/Log4jWebInitializerImplTest.java
index 7949e49..9da3ba1 100644
--- a/log4j-web/src/test/java/org/apache/logging/log4j/web/Log4jWebInitializerImplTest.java
+++ b/log4j-web/src/test/java/org/apache/logging/log4j/web/Log4jWebInitializerImplTest.java
@@ -16,9 +16,13 @@
  */
 package org.apache.logging.log4j.web;
 
+import java.net.URI;
+import java.net.URL;
+import java.util.HashSet;
 import javax.servlet.ServletContext;
-
 import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.DefaultConfiguration;
+import org.apache.logging.log4j.core.config.composite.CompositeConfiguration;
 import org.apache.logging.log4j.core.impl.ContextAnchor;
 import org.junit.Before;
 import org.junit.Rule;
@@ -30,7 +34,16 @@ import org.mockito.Captor;
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
 
-import static org.junit.Assert.*;
+import static java.util.Arrays.asList;
+import static java.util.Collections.singletonList;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.BDDMockito.given;
 import static org.mockito.BDDMockito.then;
@@ -42,7 +55,7 @@ public class Log4jWebInitializerImplTest {
     @Captor
     private ArgumentCaptor<Log4jWebLifeCycle> initializerCaptor;
     @Captor
-    private ArgumentCaptor<org.apache.logging.log4j.spi.LoggerContext> loggerContextCaptor;
+    private ArgumentCaptor<LoggerContext> loggerContextCaptor;
     @Rule
     public ExpectedException expectedException = ExpectedException.none();
 
@@ -313,4 +326,82 @@ public class Log4jWebInitializerImplTest {
 
         assertNull("The context should finally still be null.", ContextAnchor.THREAD_CONTEXT.get());
     }
+
+    @Test
+    public void testMissingLocationParameterWithNoMatchingResourceSetsNoConfigLocation() {
+        given(servletContext.getResourcePaths("/WEB-INF/")).willReturn(new HashSet<String>());
+
+        this.initializerImpl.start();
+
+        then(servletContext).should().setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE), loggerContextCaptor.capture());
+        assertNotNull("The context attribute should not be null.", loggerContextCaptor.getValue());
+
+        assertThat(loggerContextCaptor.getValue().getConfigLocation(), is(nullValue()));
+
+        this.initializerImpl.stop();
+    }
+
+    @Test
+    public void testMissingLocationParameterWithOneMatchingResourceUsesResourceConfigLocation() throws Exception {
+        given(servletContext.getResourcePaths("/WEB-INF/")).willReturn(new HashSet<>(singletonList("/WEB-INF/log4j2.xml")));
+        given(servletContext.getResource("/WEB-INF/log4j2.xml")).willReturn(new URL("file:/a/b/c/WEB-INF/log4j2.xml"));
+
+        this.initializerImpl.start();
+
+        then(servletContext).should().setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE), loggerContextCaptor.capture());
+        assertNotNull("The context attribute should not be null.", loggerContextCaptor.getValue());
+
+        assertThat(loggerContextCaptor.getValue().getConfigLocation(), is(URI.create("file:/a/b/c/WEB-INF/log4j2.xml")));
+
+        this.initializerImpl.stop();
+    }
+
+    @Test
+    public void testMissingLocationParameterWithManyMatchingResourcesUsesFirstMatchingResourceConfigLocation() throws Exception {
+        given(servletContext.getInitParameter(eq(Log4jWebSupport.LOG4J_CONTEXT_NAME))).willReturn("mycontext");
+        given(servletContext.getResourcePaths("/WEB-INF/")).willReturn(
+                new HashSet<>(asList("/WEB-INF/a.xml", "/WEB-INF/log4j2-mycontext.xml", "/WEB-INF/log4j2.xml")));
+        given(servletContext.getResource("/WEB-INF/log4j2-mycontext.xml")).willReturn(
+                new URL("file:/a/b/c/WEB-INF/log4j2-mycontext.xml"));
+
+        this.initializerImpl.start();
+
+        then(servletContext).should().setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE), loggerContextCaptor.capture());
+        assertNotNull("The context attribute should not be null.", loggerContextCaptor.getValue());
+
+        assertThat(loggerContextCaptor.getValue().getConfigLocation(),
+                is(URI.create("file:/a/b/c/WEB-INF/log4j2-mycontext.xml")));
+
+        this.initializerImpl.stop();
+    }
+
+    @Test
+    public void testCompositeLocationParameterWithEmptyUriListSetsDefaultConfiguration() {
+        given(servletContext.getInitParameter(eq(Log4jWebSupport.LOG4J_CONFIG_LOCATION))).willReturn(",,,");
+
+        this.initializerImpl.start();
+
+        then(servletContext).should().setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE), loggerContextCaptor.capture());
+        assertNotNull("The context attribute should not be null.", loggerContextCaptor.getValue());
+
+        assertThat(loggerContextCaptor.getValue().getConfiguration(), is(instanceOf(DefaultConfiguration.class)));
+
+        this.initializerImpl.stop();
+    }
+
+    @Test
+    public void testCompositeLocationParameterSetsCompositeConfiguration() {
+        given(servletContext.getInitParameter(eq(Log4jWebSupport.LOG4J_CONTEXT_NAME))).willReturn("mycontext");
+        given(servletContext.getInitParameter(eq(Log4jWebSupport.LOG4J_CONFIG_LOCATION))).willReturn(
+                "/a.txt,,/WEB-INF/log4j2-mycontext.xml");
+
+        this.initializerImpl.start();
+
+        then(servletContext).should().setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE), loggerContextCaptor.capture());
+        assertNotNull("The context attribute should not be null.", loggerContextCaptor.getValue());
+
+        assertThat(loggerContextCaptor.getValue().getConfiguration(), is(instanceOf(CompositeConfiguration.class)));
+
+        this.initializerImpl.stop();
+    }
 }


[2/3] logging-log4j2 git commit: Closes #176

Posted by rg...@apache.org.
Closes #176


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/5e4748ac
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/5e4748ac
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/5e4748ac

Branch: refs/heads/release-2.x
Commit: 5e4748acd0ce2808a54d227efe8c5c915c95c0c9
Parents: 76a7b18 e592d46
Author: Ralph Goers <rg...@apache.org>
Authored: Mon Jul 2 21:36:53 2018 -0700
Committer: Ralph Goers <rg...@apache.org>
Committed: Mon Jul 2 21:36:53 2018 -0700

----------------------------------------------------------------------
 .../log4j/web/Log4jWebInitializerImpl.java      | 20 +++-
 .../log4j/web/Log4jWebInitializerImplTest.java  | 97 +++++++++++++++++++-
 2 files changed, 113 insertions(+), 4 deletions(-)
----------------------------------------------------------------------



[3/3] logging-log4j2 git commit: LOG4J2-1721 - Allow composite configuration for context parameter

Posted by rg...@apache.org.
LOG4J2-1721 - Allow composite configuration for context parameter


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/5aef5b7d
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/5aef5b7d
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/5aef5b7d

Branch: refs/heads/release-2.x
Commit: 5aef5b7db25c7e6eb17f71e2327df82196ef5585
Parents: 5e4748a
Author: Ralph Goers <rg...@apache.org>
Authored: Mon Jul 2 21:40:20 2018 -0700
Committer: Ralph Goers <rg...@apache.org>
Committed: Mon Jul 2 21:40:20 2018 -0700

----------------------------------------------------------------------
 src/changes/changes.xml | 3 +++
 1 file changed, 3 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5aef5b7d/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 109189f..2eefe76 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -31,6 +31,9 @@
          - "remove" - Removed
     -->
     <release version="2.11.1" date="2018-MM-DD" description="GA Release 2.11.1">
+      <action issue="LOG4J2-1721" dev="rgoers" type="update" due-to="Phokham Nonava">
+        Allow composite configuration for context parameter.
+      </action>
       <action issue="LOG4J2-2343" dev="rgoers" type="fix" due-to="Raymond Augé">
         The OSGi Activator specified an incorrect version.
       </action>