Author: rgoers
Date: Mon Jun 9 22:51:54 2014
New Revision: 1601520
URL: http://svn.apache.org/r1601520
Log:
LOG4J2-554 - Allow configuration files to be located as Servlet Context
resources.
Added:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SetUtils.java
Modified:
logging/log4j/log4j2/trunk/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java
logging/log4j/log4j2/trunk/log4j-web/src/test/java/org/apache/logging/log4j/web/Log4jWebInitializerImplTest.java
logging/log4j/log4j2/trunk/src/changes/changes.xml
logging/log4j/log4j2/trunk/src/site/xdoc/manual/webapp.xml
Added:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SetUtils.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SetUtils.java?rev=1601520&view=auto
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SetUtils.java
(added)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SetUtils.java
Mon Jun 9 22:51:54 2014
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.util;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ *
+ *
+ *
+ */
+public final class SetUtils {
+ private SetUtils() {};
+
+ public static String[] prefixSet(Set<String> set, String prefix) {
+ Set<String> prefixSet = new HashSet<String>();
+ for (String str : set) {
+ if (str.startsWith(prefix)) {
+ prefixSet.add(str);
+ }
+ }
+ return prefixSet.toArray(new String[prefixSet.size()]);
+ }
+}
Modified:
logging/log4j/log4j2/trunk/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java?rev=1601520&r1=1601519&r2=1601520&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java
Mon Jun 9 22:51:54 2014
@@ -16,8 +16,11 @@
*/
package org.apache.logging.log4j.web;
+import java.net.MalformedURLException;
import java.net.URI;
+import java.net.URL;
import java.util.Map;
+import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.ServletContext;
@@ -35,6 +38,7 @@ import org.apache.logging.log4j.core.sel
import org.apache.logging.log4j.core.util.FileUtils;
import org.apache.logging.log4j.core.util.Loader;
import org.apache.logging.log4j.core.util.NetUtils;
+import org.apache.logging.log4j.core.util.SetUtils;
import org.apache.logging.log4j.spi.LoggerContextFactory;
/**
@@ -92,14 +96,7 @@ final class Log4jWebInitializerImpl exte
}
private void initializeJndi(final String location) {
- URI configLocation = null;
- if (location != null) {
- try {
- configLocation = FileUtils.getCorrectedFilePathUri(location);
- } catch (final Exception e) {
- this.servletContext.log("Unable to convert configuration
location [" + location + "] to a URI!", e);
- }
- }
+ URI configLocation = getConfigURI(location);;
if (this.name == null) {
throw new IllegalStateException("A log4jContextName context
parameter is required");
@@ -141,7 +138,48 @@ final class Log4jWebInitializerImpl exte
return;
}
- this.loggerContext = Configurator.initialize(this.name,
this.getClassLoader(), location, this.servletContext);
+ URI uri = getConfigURI(location);
+ this.loggerContext = Configurator.initialize(this.name,
this.getClassLoader(), uri, this.servletContext);
+ }
+
+ private URI getConfigURI(final String location) {
+ try {
+ String configLocation = location;
+ if (configLocation == null) {
+ String[] paths =
SetUtils.prefixSet(servletContext.getResourcePaths("/WEB-INF/"),
"/WEB-INF/log4j2");
+ if (paths.length == 1) {
+ configLocation = paths[0];
+ } else if (paths.length > 1) {
+ final String prefix = "/WEB-INF/log4j2-" + this.name + ".";
+ boolean found = false;
+ for (String str : paths) {
+ if (str.startsWith(prefix)) {
+ configLocation = str;
+ break;
+ }
+ }
+ if (!found) {
+ configLocation = paths[0];
+ }
+ }
+ }
+ if (configLocation != null) {
+ URL url = servletContext.getResource(configLocation);
+ if (url != null) {
+ return url.toURI();
+ }
+ }
+ } catch (Exception ex) {
+ // Just try passing the location.
+ }
+ if (location != null) {
+ try {
+ return FileUtils.getCorrectedFilePathUri(location);
+ } catch (final Exception e) {
+ this.servletContext.log("Unable to convert configuration
location [" + location + "] to a URI!", e);
+ }
+ }
+ return null;
}
@Override
Modified:
logging/log4j/log4j2/trunk/log4j-web/src/test/java/org/apache/logging/log4j/web/Log4jWebInitializerImplTest.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-web/src/test/java/org/apache/logging/log4j/web/Log4jWebInitializerImplTest.java?rev=1601520&r1=1601519&r2=1601520&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-web/src/test/java/org/apache/logging/log4j/web/Log4jWebInitializerImplTest.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-web/src/test/java/org/apache/logging/log4j/web/Log4jWebInitializerImplTest.java
Mon Jun 9 22:51:54 2014
@@ -105,6 +105,7 @@ public class Log4jWebInitializerImplTest
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn(null);
expect(this.servletContext.getServletContextName()).andReturn("helloWorld01");
+
expect(this.servletContext.getResourcePaths("/WEB-INF/")).andReturn(null);
this.servletContext.setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE),
capture(loggerContextCapture));
expectLastCall();
@@ -168,6 +169,7 @@ public class Log4jWebInitializerImplTest
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn("false");
expect(this.servletContext.getServletContextName()).andReturn("helloWorld02");
+
expect(this.servletContext.getResourcePaths("/WEB-INF/")).andReturn(null);
expect(this.servletContext.getClassLoader()).andReturn(this.getClass().getClassLoader());
this.servletContext.setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE),
capture(loggerContextCapture));
expectLastCall();
@@ -232,6 +234,7 @@ public class Log4jWebInitializerImplTest
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn("nothing");
expect(this.servletContext.getServletContextName()).andReturn("helloWorld03");
+
expect(this.servletContext.getResourcePaths("/WEB-INF/")).andReturn(null);
expect(this.servletContext.getClassLoader()).andReturn(this.getClass().getClassLoader());
this.servletContext.setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE),
capture(loggerContextCapture));
expectLastCall();
@@ -276,6 +279,7 @@ public class Log4jWebInitializerImplTest
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn(null);
expect(this.servletContext.getServletContextName()).andReturn("helloWorld04");
+
expect(this.servletContext.getResourcePaths("/WEB-INF/")).andReturn(null);
expect(this.servletContext.getClassLoader()).andReturn(this.getClass().getClassLoader());
this.servletContext.setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE),
capture(loggerContextCapture));
expectLastCall();
@@ -319,6 +323,7 @@ public class Log4jWebInitializerImplTest
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn(null);
expect(this.servletContext.getServletContextName()).andReturn("helloWorld05");
+
expect(this.servletContext.getResourcePaths("/WEB-INF/")).andReturn(null);
expect(this.servletContext.getClassLoader()).andReturn(this.getClass().getClassLoader());
this.servletContext.setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE),
capture(loggerContextCapture));
expectLastCall();
@@ -354,7 +359,7 @@ public class Log4jWebInitializerImplTest
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION)).andReturn(null);
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn("true");
-
+
expect(this.servletContext.getResourcePaths("/WEB-INF/")).andReturn(null);
replay(this.servletContext);
assertNull("The context should be null.",
ContextAnchor.THREAD_CONTEXT.get());
@@ -375,6 +380,7 @@ public class Log4jWebInitializerImplTest
expect(this.servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION)).andReturn(null);
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn("true");
+
expect(this.servletContext.getResourcePaths("/WEB-INF/")).andReturn(null);
this.servletContext.log(anyObject(String.class));
expectLastCall();
this.servletContext.setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE),
capture(loggerContextCapture));
@@ -428,6 +434,7 @@ public class Log4jWebInitializerImplTest
expect(this.servletContext.getInitParameter(Log4jWebSupport.IS_LOG4J_CONTEXT_SELECTOR_NAMED))
.andReturn(null);
expect(this.servletContext.getServletContextName()).andReturn("helloWorld01");
+
expect(this.servletContext.getResourcePaths("/WEB-INF/")).andReturn(null);
this.servletContext.setAttribute(eq(Log4jWebSupport.CONTEXT_ATTRIBUTE),
capture(loggerContextCapture));
expectLastCall();
Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1601520&r1=1601519&r2=1601520&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Mon Jun 9 22:51:54 2014
@@ -22,6 +22,9 @@
</properties>
<body>
<release version="2.0-rc2" date="2014-MM-DD" description="Bug fixes and
enhancements">
+ <action issue="LOG4J2-554" dev="rgoers" type="update">
+ Allow configuration files to be located as Servlet Context resources.
+ </action>
<action issue="LOG4J2-535" dev="rgoers" type="fix">
Reset rollover time when size rollover is triggered.
</action>
Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/webapp.xml
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/webapp.xml?rev=1601520&r1=1601519&r2=1601520&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/webapp.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/webapp.xml Mon Jun 9
22:51:54 2014
@@ -40,14 +40,26 @@
<a href="../maven-artifacts.html">Maven, Ivy, and Gradle Artifacts</a>
manual page.
</p>
<p class="big-red">
- It is important to note that you should add
<code>shutdownHook="disable"</code> to your root
- configuration element to avoid memory leaks. For example:
- </p>
- <pre class="prettyprint"><![CDATA[
-<Configuration shutdownHook="disable">
- <!-- ... -->
-</Configuration>
- ]]></pre>
+ To avoid problems the Log4j shutdown hook will automatically be
disabled when the log4j-web jar is included.
+ <a name="Configuration"/>
+ <subsection name="Configuration">
+ <p>Log4j allows the configuration file to be specified in web.xml
using the log4jConfiguration context parameter.
+ Log4j will search for configuration files by:
+ <ol>
+ <li>If a location is provided it will be searched for as a
servlet context resource. For example,
+ if log4jConfiguration contains "logging.xml" then Log4j
will look for a file with that name in the
+ root directory of the web application.
+ </li>
+ <li>If no location is defined Log4j will search for a file
that starts with "log4j2" in the WEB-INF directory.
+ If more than one file is found then if a file that starts
with "log4j2-<i>name</i>", where <i>name</i> is the
+ name of the web application, is present it will be used.
Otherwise the first file will be used.
+ </li>
+ <li>The "normal" search sequence using the classpath and file
URLs will be used to locate the configuration
+ file.
+ </li>
+ </ol>
+ </p>
+ </subsection>
<subsection name="Servlet 3.0 and Newer Web Applications">
<a name="Servlet-3.0" />
<p>