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 2020/04/13 15:33:43 UTC
[logging-log4j2] branch master updated: LOG4J2-2520 - Allow servlet
context path to be retrive with /
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new ef52626 LOG4J2-2520 - Allow servlet context path to be retrive with /
ef52626 is described below
commit ef5262646ae5cd82874a8fa6afb7767fec917466
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Mon Apr 13 08:33:27 2020 -0700
LOG4J2-2520 - Allow servlet context path to be retrive with /
---
.../main/java/org/apache/logging/log4j/web/WebLookup.java | 15 +++++++++++++++
.../java/org/apache/logging/log4j/web/WebLookupTest.java | 9 +++++++++
src/changes/changes.xml | 3 +++
src/site/asciidoc/manual/lookups.adoc | 3 +++
4 files changed, 30 insertions(+)
diff --git a/log4j-web/src/main/java/org/apache/logging/log4j/web/WebLookup.java b/log4j-web/src/main/java/org/apache/logging/log4j/web/WebLookup.java
index 1f9ce95..c58ceec 100644
--- a/log4j-web/src/main/java/org/apache/logging/log4j/web/WebLookup.java
+++ b/log4j-web/src/main/java/org/apache/logging/log4j/web/WebLookup.java
@@ -164,6 +164,21 @@ public class WebLookup extends AbstractLookup {
return root;
}
+ if ("contextPathName".equals(key)) {
+ String path = ctx.getContextPath();
+ if (path.trim().contains("/")) {
+ String[] fields = path.split("/");
+ for (String field : fields) {
+ if (field.length() > 0) {
+ return field;
+ }
+ }
+ return null;
+
+ }
+ return ctx.getContextPath();
+ }
+
if ("contextPath".equals(key)) {
return ctx.getContextPath();
}
diff --git a/log4j-web/src/test/java/org/apache/logging/log4j/web/WebLookupTest.java b/log4j-web/src/test/java/org/apache/logging/log4j/web/WebLookupTest.java
index 99a3132..a95e01d 100644
--- a/log4j-web/src/test/java/org/apache/logging/log4j/web/WebLookupTest.java
+++ b/log4j-web/src/test/java/org/apache/logging/log4j/web/WebLookupTest.java
@@ -38,6 +38,7 @@ public class WebLookupTest {
public void testLookup() throws Exception {
ContextAnchor.THREAD_CONTEXT.remove();
final ServletContext servletContext = new MockServletContext();
+ ((MockServletContext) servletContext).setContextPath("/WebApp");
servletContext.setAttribute("TestAttr", "AttrValue");
servletContext.setInitParameter("TestParam", "ParamValue");
servletContext.setAttribute("Name1", "Ben");
@@ -65,6 +66,9 @@ public class WebLookupTest {
value = substitutor.replace("${web:Name2}");
assertNotNull("No value for Name2", value);
assertEquals("Incorrect value for Name2: " + value, "Jerry", value);
+ value = substitutor.replace("${web:contextPathName}");
+ assertNotNull("No value for context name", value);
+ assertEquals("Incorrect value for context name", "WebApp", value);
} catch (final IllegalStateException e) {
fail("Failed to initialize Log4j properly." + e.getMessage());
}
@@ -76,6 +80,7 @@ public class WebLookupTest {
public void testLookup2() throws Exception {
ContextAnchor.THREAD_CONTEXT.remove();
final ServletContext servletContext = new MockServletContext();
+ ((MockServletContext) servletContext).setContextPath("/");
servletContext.setAttribute("TestAttr", "AttrValue");
servletContext.setInitParameter("myapp.logdir", "target");
servletContext.setAttribute("Name1", "Ben");
@@ -96,6 +101,10 @@ public class WebLookupTest {
assertEquals("target/myapp.log", fa.getFileName());
}
}
+ final StrSubstitutor substitutor = config.getStrSubstitutor();
+ String value = substitutor.replace("${web:contextPathName:-default}");
+ assertNotNull("No value for context name", value);
+ assertEquals("Incorrect value for context name", "default", value);
initializer.stop();
ContextAnchor.THREAD_CONTEXT.remove();
}
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b08903a..457c5e6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -169,6 +169,9 @@
</action>
</release>
<release version="2.13.2" date="2020-MM-DD" description="GA Release 2.13.2">
+ <action issue="LOG4J2-2520" dev="rgoers" type="update">
+ Allow servlet context path to be retrieved without "/".
+ </action>
<action issue="LOG4J2-2818" dev="rgoers" type="update">
Allow Spring Lookup to return default and active profiles.
</action>
diff --git a/src/site/asciidoc/manual/lookups.adoc b/src/site/asciidoc/manual/lookups.adoc
index f910737..06b6c6b 100644
--- a/src/site/asciidoc/manual/lookups.adoc
+++ b/src/site/asciidoc/manual/lookups.adoc
@@ -726,6 +726,9 @@ retrieved:
|contextPath
|The context path of the web application
+|contextPathName
+|The first token in the context path of the web application splitting on "/" characters.
+
|effectiveMajorVersion
|Gets the major version of the Servlet specification that the application
represented by this ServletContext is based on.