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 2012/09/09 00:23:27 UTC
svn commit: r1382381 - in /logging/log4j/log4j2/trunk: ./
api/src/main/java/org/apache/logging/log4j/
api/src/main/java/org/apache/logging/log4j/spi/
api/src/test/java/org/apache/logging/log4j/ core/
core/src/main/java/org/apache/logging/log4j/core/con...
Author: rgoers
Date: Sat Sep 8 22:23:25 2012
New Revision: 1382381
URL: http://svn.apache.org/viewvc?rev=1382381&view=rev
Log:
ClassLoading fixes
Added:
logging/log4j/log4j2/trunk/web/
logging/log4j/log4j2/trunk/web/pom.xml
logging/log4j/log4j2/trunk/web/src/
logging/log4j/log4j2/trunk/web/src/main/
logging/log4j/log4j2/trunk/web/src/main/java/
logging/log4j/log4j2/trunk/web/src/main/java/org/
logging/log4j/log4j2/trunk/web/src/main/java/org/apache/
logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/
logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/
logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/
logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/
- copied from r1382131, logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/javaee/
logging/log4j/log4j2/trunk/web/src/site/
logging/log4j/log4j2/trunk/web/src/site/site.xml
logging/log4j/log4j2/trunk/web/src/site/xdoc/
logging/log4j/log4j2/trunk/web/src/site/xdoc/index.xml
logging/log4j/log4j2/trunk/web/src/test/
logging/log4j/log4j2/trunk/web/src/test/java/
logging/log4j/log4j2/trunk/web/src/test/java/org/
logging/log4j/log4j2/trunk/web/src/test/java/org/apache/
logging/log4j/log4j2/trunk/web/src/test/java/org/apache/logging/
logging/log4j/log4j2/trunk/web/src/test/java/org/apache/logging/log4j/
logging/log4j/log4j2/trunk/web/src/test/java/org/apache/logging/log4j/core/
logging/log4j/log4j2/trunk/web/src/test/java/org/apache/logging/log4j/core/web/
- copied from r1382131, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/javaee/
logging/log4j/log4j2/trunk/web/src/test/resources/
logging/log4j/log4j2/trunk/web/src/test/resources/log4j2-config.xml
- copied unchanged from r1382131, logging/log4j/log4j2/trunk/core/src/test/resources/log4j2-config.xml
Removed:
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/javaee/
logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/javaee/
logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/ContextAnchor.java
Modified:
logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/LogManager.java
logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/spi/LoggerContextFactory.java
logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/SimpleLoggerContextFactory.java
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/config/plugins/ResolverUtil.java
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Loader.java
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/BasicContextSelector.java
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/ContextSelector.java
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/JNDIContextSelector.java
logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
logging/log4j/log4j2/trunk/pom.xml
logging/log4j/log4j2/trunk/src/changes/changes.xml
logging/log4j/log4j2/trunk/src/site/site.xml
logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/JNDIContextFilter.java
logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/Log4jContextListener.java
logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/package-info.java
logging/log4j/log4j2/trunk/web/src/test/java/org/apache/logging/log4j/core/web/Log4jContextListenerTest.java
Modified: logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/LogManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/LogManager.java?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/LogManager.java (original)
+++ logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/LogManager.java Sat Sep 8 22:23:25 2012
@@ -126,7 +126,7 @@ public class LogManager {
*/
public static Logger getLogger(String name) {
- return factory.getContext(LogManager.class.getName(), false).getLogger(name);
+ return factory.getContext(LogManager.class.getName(), null, false).getLogger(name);
}
/**
@@ -135,7 +135,7 @@ public class LogManager {
* @return The Logger.
*/
public static Logger getLogger(Class clazz) {
- return factory.getContext(LogManager.class.getName(), false).getLogger(clazz.getName());
+ return factory.getContext(LogManager.class.getName(), null, false).getLogger(clazz.getName());
}
/**
@@ -147,7 +147,7 @@ public class LogManager {
*/
protected static Logger getLogger(String fqcn, String name) {
- return factory.getContext(fqcn, false).getLogger(name);
+ return factory.getContext(fqcn, null, false).getLogger(name);
}
/**
@@ -158,7 +158,7 @@ public class LogManager {
* @return The current LoggerContext.
*/
public static LoggerContext getContext() {
- return factory.getContext(LogManager.class.getName(), true);
+ return factory.getContext(LogManager.class.getName(), null, true);
}
/**
@@ -171,10 +171,26 @@ public class LogManager {
* @return a LoggerContext.
*/
public static LoggerContext getContext(boolean currentContext) {
- return factory.getContext(LogManager.class.getName(), currentContext);
+ return factory.getContext(LogManager.class.getName(), null, currentContext);
}
/**
+ * Returns a LoggerContext.
+ *
+ * @param loader The ClassLoader for the context. If null the context will attempt to determine the appropriate
+ * ClassLoader.
+ * @param currentContext if false the LoggerContext appropriate for the caller of this method is returned. For
+ * example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
+ * returned and if the caller is a class in the container's classpath then a different LoggerContext may be
+ * returned. If true then only a single LoggerContext will be returned.
+ * @return a LoggerContext.
+ */
+ public static LoggerContext getContext(ClassLoader loader, boolean currentContext) {
+ return factory.getContext(LogManager.class.getName(), loader, currentContext);
+ }
+
+
+ /**
* Returns a LoggerContext
* @param fqcn The fully qualified class name of the Class that this method is a member of.
* @param currentContext if false the LoggerContext appropriate for the caller of this method is returned. For
@@ -184,7 +200,23 @@ public class LogManager {
* @return a LoggerContext.
*/
protected static LoggerContext getContext(String fqcn, boolean currentContext) {
- return factory.getContext(fqcn, currentContext);
+ return factory.getContext(fqcn, null, currentContext);
+ }
+
+
+ /**
+ * Returns a LoggerContext
+ * @param fqcn The fully qualified class name of the Class that this method is a member of.
+ * @param loader The ClassLoader for the context. If null the context will attempt to determine the appropriate
+ * ClassLoader.
+ * @param currentContext if false the LoggerContext appropriate for the caller of this method is returned. For
+ * example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
+ * returned and if the caller is a class in the container's classpath then a different LoggerContext may be
+ * returned. If true then only a single LoggerContext will be returned.
+ * @return a LoggerContext.
+ */
+ protected static LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext) {
+ return factory.getContext(fqcn, loader, currentContext);
}
private static ClassLoader findClassLoader() {
@@ -200,6 +232,9 @@ public class LogManager {
}
);
}
+ if (cl != null && cl.getParent() != null) {
+
+ }
if (cl == null) {
cl = LogManager.class.getClassLoader();
}
Modified: logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/spi/LoggerContextFactory.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/spi/LoggerContextFactory.java?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/spi/LoggerContextFactory.java (original)
+++ logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/spi/LoggerContextFactory.java Sat Sep 8 22:23:25 2012
@@ -23,9 +23,10 @@ public interface LoggerContextFactory {
/**
* @param fqcn The fully qualified class name of the caller.
+ * @param loader The ClassLoader to use or null.
* @param currentContext If true returns the current Context, if false returns the Context appropriate
* for the caller if a more appropriate Context can be determined.
* @return The LoggerContext.
*/
- LoggerContext getContext(String fqcn, boolean currentContext);
+ LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext);
}
Modified: logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/SimpleLoggerContextFactory.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/SimpleLoggerContextFactory.java?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/SimpleLoggerContextFactory.java (original)
+++ logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/SimpleLoggerContextFactory.java Sat Sep 8 22:23:25 2012
@@ -26,7 +26,7 @@ public class SimpleLoggerContextFactory
private static LoggerContext ctx = new SimpleLoggerContext();
- public LoggerContext getContext(String FQCN, boolean currentContext) {
+ public LoggerContext getContext(String FQCN, ClassLoader loader, boolean currentContext) {
return ctx;
}
}
Modified: logging/log4j/log4j2/trunk/core/pom.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/pom.xml?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/pom.xml (original)
+++ logging/log4j/log4j2/trunk/core/pom.xml Sat Sep 8 22:23:25 2012
@@ -57,12 +57,6 @@
<scope>test</scope>
</dependency>
<dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.4</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
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=1382381&r1=1382380&r2=1382381&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 Sat Sep 8 22:23:25 2012
@@ -25,17 +25,24 @@ import java.net.URI;
import java.net.URL;
/**
- *
+ * Class that can be used to initialize and configure the Logging system.
*/
public final class Configurator {
private Configurator() {
}
- public static LoggerContext intitalize(String name, String configLocation) {
+ /**
+ * Initialize 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.
+ * @return
+ */
+ public static LoggerContext intitalize(String name, ClassLoader loader, String configLocation) {
try {
- LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+ LoggerContext ctx = (LoggerContext) LogManager.getContext(loader, false);
URI uri = configLocation == null ? null : new URI(configLocation);
Configuration config = ConfigurationFactory.getInstance().getConfiguration(name, uri);
ctx.setConfiguration(config);
Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/ResolverUtil.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/ResolverUtil.java?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/ResolverUtil.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/ResolverUtil.java Sat Sep 8 22:23:25 2012
@@ -17,6 +17,7 @@
package org.apache.logging.log4j.core.config.plugins;
import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.helpers.Loader;
import org.apache.logging.log4j.status.StatusLogger;
import java.io.File;
@@ -113,7 +114,7 @@ public class ResolverUtil<T> {
* @return the ClassLoader that will be used to scan for classes
*/
public ClassLoader getClassLoader() {
- return classloader == null ? Thread.currentThread().getContextClassLoader() : classloader;
+ return classloader != null ? classloader : (classloader = Loader.getClassLoader(ResolverUtil.class, null));
}
/**
Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Loader.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Loader.java?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Loader.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Loader.java Sat Sep 8 22:23:25 2012
@@ -219,22 +219,42 @@ public final class Loader {
}
}
+ public static ClassLoader getClassLoader(Class class1, Class class2) {
+
+ ClassLoader loader1 = null;
+ try {
+ loader1 = getTCL();
+ } catch (Exception ex) {
+ LOGGER.warn("Caught exception locating thread ClassLoader {}", ex.getMessage());
+ }
+ ClassLoader loader2 = class1 == null ? null : class1.getClassLoader();
+ ClassLoader loader3 = class2 == null ? null : class2.getClass().getClassLoader();
+
+ if (isChild(loader1, loader2)) {
+ return isChild(loader1, loader3) ? loader1 : loader3;
+ } else {
+ return isChild(loader2, loader3) ? loader2 : loader3;
+ }
+ }
+
+ private static boolean isChild(ClassLoader loader1, ClassLoader loader2) {
+ if (loader1 != null && loader2 != null) {
+ ClassLoader parent = loader1.getParent();
+ while (parent != null && parent != loader2) {
+ parent = parent.getParent();
+ }
+ return parent != null;
+ }
+ return loader1 != null;
+ }
+
/**
* Return the ClassLoader to use.
* @return the ClassLoader.
*/
public static ClassLoader getClassLoader() {
- ClassLoader cl = null;
- try {
- cl = getTCL();
- } catch (Exception ex) {
- // Ignore the exception. The ClassLoader will be located.
- }
- if (cl == null) {
- cl = Loader.getClassLoader();
- }
- return cl;
+ return getClassLoader(Loader.class, null);
}
private static ClassLoader getTCL() throws IllegalAccessException, InvocationTargetException {
Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java Sat Sep 8 22:23:25 2012
@@ -64,12 +64,13 @@ public class Log4jContextFactory impleme
/**
* Load the LoggerContext using the ContextSelector.
* @param fqcn The fully qualified class name of the caller.
+ * @param loader The ClassLoader to use or null.
* @param currentContext If true returns the current Context, if false returns the Context appropriate
* for the caller if a more appropriate Context can be determined.
* @return The LoggerContext.
*/
- public LoggerContext getContext(String fqcn, boolean currentContext) {
- LoggerContext ctx = selector.getContext(fqcn, currentContext);
+ public LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext) {
+ LoggerContext ctx = selector.getContext(fqcn, loader, currentContext);
if (ctx.getStatus() == LoggerContext.Status.INITIALIZED) {
ctx.start();
}
Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/BasicContextSelector.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/BasicContextSelector.java?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/BasicContextSelector.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/BasicContextSelector.java Sat Sep 8 22:23:25 2012
@@ -17,7 +17,7 @@
package org.apache.logging.log4j.core.selector;
import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.javaee.ContextAnchor;
+import org.apache.logging.log4j.core.impl.ContextAnchor;
import java.util.ArrayList;
import java.util.Collections;
@@ -30,7 +30,7 @@ public class BasicContextSelector implem
private static LoggerContext context = new LoggerContext("Default");
- public LoggerContext getContext(String fqcn, boolean currentContext) {
+ public LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext) {
LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
return ctx != null ? ctx : context;
Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java Sat Sep 8 22:23:25 2012
@@ -18,7 +18,7 @@ package org.apache.logging.log4j.core.se
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.helpers.Loader;
-import org.apache.logging.log4j.core.javaee.ContextAnchor;
+import org.apache.logging.log4j.core.impl.ContextAnchor;
import org.apache.logging.log4j.status.StatusLogger;
import java.lang.ref.WeakReference;
@@ -62,7 +62,7 @@ public class ClassLoaderContextSelector
setupCallerCheck();
}
- public LoggerContext getContext(String fqcn, boolean currentContext) {
+ public LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext) {
if (currentContext) {
LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
@@ -70,6 +70,8 @@ public class ClassLoaderContextSelector
return ctx;
}
return getDefault();
+ } else if (loader != null) {
+ return locateContext(loader, null);
} else {
if (getCallerClass != null) {
try {
Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/ContextSelector.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/ContextSelector.java?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/ContextSelector.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/selector/ContextSelector.java Sat Sep 8 22:23:25 2012
@@ -28,11 +28,12 @@ public interface ContextSelector {
/**
* Return the LoggerContext.
* @param fqcn The fully qualified class name of the caller.
+ * @param loader ClassLoader to use or null.
* @param currentContext If true returns the current Context, if false returns the Context appropriate
* for the caller if a more appropriate Context can be determined.
* @return The LoggerContext.
*/
- LoggerContext getContext(String fqcn, boolean currentContext);
+ LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext);
/**
* Return a List of all the available LoggerContexts.
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=1382381&r1=1382380&r2=1382381&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 Sat Sep 8 22:23:25 2012
@@ -18,7 +18,7 @@ package org.apache.logging.log4j.core.se
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.helpers.Constants;
-import org.apache.logging.log4j.core.javaee.ContextAnchor;
+import org.apache.logging.log4j.core.impl.ContextAnchor;
import org.apache.logging.log4j.status.StatusLogger;
import javax.naming.Context;
@@ -93,7 +93,7 @@ public class JNDIContextSelector impleme
private static final StatusLogger LOGGER = StatusLogger.getLogger();
- public LoggerContext getContext(String fqcn, boolean currentContext) {
+ public LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext) {
LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
if (lc != null) {
Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java (original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java Sat Sep 8 22:23:25 2012
@@ -41,7 +41,7 @@ public class TestConfigurator {
@Test
public void testFromFile() throws Exception {
- Configurator.intitalize("Test1", "target/test-classes/log4j2-config.xml");
+ Configurator.intitalize("Test1", null, "target/test-classes/log4j2-config.xml");
Logger logger = LogManager.getLogger("org.apache.test.TestConfigurator");
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
@@ -59,7 +59,7 @@ public class TestConfigurator {
@Test
public void testFromClassPath() throws Exception {
- Configurator.intitalize("Test1", "log4j2-config.xml");
+ Configurator.intitalize("Test1", null, "log4j2-config.xml");
Logger logger = LogManager.getLogger("org.apache.test.TestConfigurator");
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
@@ -77,7 +77,7 @@ public class TestConfigurator {
@Test
public void testByName() throws Exception {
- Configurator.intitalize("-config", null);
+ Configurator.intitalize("-config", null, null);
Logger logger = LogManager.getLogger("org.apache.test.TestConfigurator");
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
@@ -97,7 +97,7 @@ public class TestConfigurator {
public void testReconfiguration() throws Exception {
File file = new File("target/test-classes/log4j2-config.xml");
file.setLastModified(System.currentTimeMillis() - 120000);
- Configurator.intitalize("Test1", "target/test-classes/log4j2-config.xml");
+ Configurator.intitalize("Test1", null, "target/test-classes/log4j2-config.xml");
Logger logger = LogManager.getLogger("org.apache.test.TestConfigurator");
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
Modified: logging/log4j/log4j2/trunk/pom.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/pom.xml?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/pom.xml (original)
+++ logging/log4j/log4j2/trunk/pom.xml Sat Sep 8 22:23:25 2012
@@ -435,6 +435,7 @@
<module>slf4j-impl</module>
<module>jcl-bridge</module>
<module>flume-ng</module>
+ <module>web</module>
</modules>
<profiles>
<profile>
Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sat Sep 8 22:23:25 2012
@@ -23,6 +23,11 @@
<body>
<release version="2.0-alpha3" date="TBD" description="">
+ <action dev="rgoers" type="fix">
+ Created web module to allow web applications to include the Log4j context listener in WEB-INF/lib even if
+ Log4j is in the container's class path. Allow locating the LoggerContext to include the ClassLoader. Updated
+ the Loader utility to always choose the child ClassLoader. Verified in Jboss 5 and Tomcat.
+ </action>
<action issue="LOG4J2-82" dev="rgoers" type="fix">
MarkerFilter called MarkerManager.getMarker causing the Marker to be created during the processing of the
configuration. This prevents the application from creating the Marker with any parents. MarkerWrapper in
Modified: logging/log4j/log4j2/trunk/src/site/site.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/site.xml?rev=1382381&r1=1382380&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/site.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/site.xml Sat Sep 8 22:23:25 2012
@@ -125,11 +125,6 @@
<item name="Custom Plugins" href="/manual/extending.html#Custom"/>
</item>
</menu>
- <menu name="Logging Adapters" inherit="top">
- <item name="Log4j 1.x API" href="log4j12-api/api.html"/>
- <item name="Commons Logging" href="log4j2-jcl/api.html"/>
- <item name="SLF4J" href="slf4j-impl/api.html"/>
- </menu>
<menu name="Components" inherit="top">
<item name="API" href="log4j-api/index.html"/>
<item name="Impl" href="log4j-core/index.html"/>
@@ -137,6 +132,7 @@
<item name="Commons Logging Bridge" href="log4j-jcl/index.html"/>
<item name="SLF4J Binding" href="slf4j-impl/index.html"/>
<item name="Apache Flume" href="log4j-flume-ng/index.html"/>
+ <item name="Log4j Web" href="log4j-web/index.html"/>
</menu>
<links>
<item name="Logging Wiki" href="http://wiki.apache.org/logging"/>
Added: logging/log4j/log4j2/trunk/web/pom.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/web/pom.xml?rev=1382381&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/web/pom.xml (added)
+++ logging/log4j/log4j2/trunk/web/pom.xml Sat Sep 8 22:23:25 2012
@@ -0,0 +1,190 @@
+<!--
+ 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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>2.0-alpha3-SNAPSHOT</version>
+ <relativePath>../</relativePath>
+ </parent>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-web</artifactId>
+ <packaging>jar</packaging>
+ <name>Log4j Web Adapters</name>
+ <description>Log4j Adpaters for Web Applications</description>
+ <properties>
+ <log4jParentDir>${basedir}/..</log4jParentDir>
+ <docLabel>Log4j Web Application Documentation</docLabel>
+ <projectDir>/web</projectDir>
+ </properties>
+ <dependencies>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-ext</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.4</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ <reporting>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-changes-plugin</artifactId>
+ <version>2.6</version>
+ <reportSets>
+ <reportSet>
+ <reports>
+ <report>changes-report</report>
+ </reports>
+ </reportSet>
+ </reportSets>
+ <configuration>
+ <issueLinkTemplate>%URL%/show_bug.cgi?id=%ISSUE%</issueLinkTemplate>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-checkstyle-plugin</artifactId>
+ <version>2.7</version>
+ <configuration>
+ <!--<propertiesLocation>${vfs.parent.dir}/checkstyle.properties</propertiesLocation> -->
+ <configLocation>${log4jParentDir}/checkstyle.xml</configLocation>
+ <suppressionsLocation>${log4jParentDir}/checkstyle-suppressions.xml</suppressionsLocation>
+ <enableRulesSummary>false</enableRulesSummary>
+ <propertyExpansion>basedir=${basedir}</propertyExpansion>
+ <propertyExpansion>licensedir=${log4jParentDir}/checkstyle-header.txt</propertyExpansion>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-javadoc-plugin</artifactId>
+ <version>2.8</version>
+ <configuration>
+ <bottom>Copyright © {inceptionYear}-{currentYear} {organizationName}. All Rights Reserved. Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, and the
+ Apache Logging project logo are trademarks of The Apache Software Foundation.</bottom>
+ <!-- module link generation is completely broken in the javadoc plugin for a multi-module non-aggregating
+ project -->
+ <detectOfflineLinks>false</detectOfflineLinks>
+ <linksource>true</linksource>
+ <tags>
+ <tag>
+ <name>issue</name>
+ <placement>a</placement>
+ <head>JIRA issue:</head>
+ </tag>
+ <tag>
+ <name>doubt</name>
+ <placement>a</placement>
+ <head>Troublesome:</head>
+ </tag>
+ <tag>
+ <name>compare</name>
+ <placement>a</placement>
+ <head>Compare with:</head>
+ </tag>
+ </tags>
+ </configuration>
+ <reportSets>
+ <reportSet>
+ <id>non-aggregate</id>
+ <reports>
+ <report>javadoc</report>
+ </reports>
+ </reportSet>
+ </reportSets>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>findbugs-maven-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <threshold>Normal</threshold>
+ <effort>Default</effort>
+ <excludeFilterFile>findbugs-exclude-filter.xml</excludeFilterFile>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jxr-plugin</artifactId>
+ <version>2.3</version>
+ <reportSets>
+ <reportSet>
+ <id>non-aggregate</id>
+ <reports>
+ <report>jxr</report>
+ </reports>
+ </reportSet>
+ <reportSet>
+ <id>aggregate</id>
+ <reports>
+ <report>aggregate</report>
+ </reports>
+ </reportSet>
+ </reportSets>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-pmd-plugin</artifactId>
+ <version>${pmd.plugin.version}</version>
+ <configuration>
+ <targetJdk>1.5</targetJdk>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>cobertura-maven-plugin</artifactId>
+ <version>2.2</version>
+ <reportSets>
+ <reportSet>
+ <!-- Disabled at it kills the site generation via a NoClassDefFoundError -->
+ <reports />
+ </reportSet>
+ </reportSets>
+ </plugin>
+ </plugins>
+ </reporting>
+</project>
+
Modified: logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/JNDIContextFilter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/JNDIContextFilter.java?rev=1382381&r1=1382131&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/JNDIContextFilter.java (original)
+++ logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/JNDIContextFilter.java Sat Sep 8 22:23:25 2012
@@ -14,9 +14,10 @@
* See the license for the specific language governing permissions and
* limitations under the license.
*/
-package org.apache.logging.log4j.core.javaee;
+package org.apache.logging.log4j.core.web;
import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.impl.ContextAnchor;
import org.apache.logging.log4j.core.impl.Log4jContextFactory;
import org.apache.logging.log4j.core.selector.ContextSelector;
import org.apache.logging.log4j.core.LoggerContext;
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=1382381&r1=1382131&r2=1382381&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 Sat Sep 8 22:23:25 2012
@@ -14,14 +14,15 @@
* See the license for the specific language governing permissions and
* limitations under the license.
*/
-package org.apache.logging.log4j.core.javaee;
+package org.apache.logging.log4j.core.web;
-import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.helpers.Loader;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
+import java.lang.reflect.Method;
/**
* Saves the LoggerContext into the ServletContext as an attribute.
@@ -37,6 +38,8 @@ public class Log4jContextListener implem
public static final String LOG4J_CONTEXT_NAME = "log4jContextName";
+ private static final String CLASSES = "WEB-INF/classes";
+
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
String locn = context.getInitParameter(LOG4J_CONFIG);
@@ -48,11 +51,32 @@ public class Log4jContextListener implem
context.log("No Log4j context configuration provided");
return;
}
- context.setAttribute(LOG4J_CONTEXT_ATTRIBUTE, Configurator.intitalize(name, locn));
+ context.setAttribute(LOG4J_CONTEXT_ATTRIBUTE, Configurator.intitalize(name, getClassLoader(context), locn));
}
public void contextDestroyed(ServletContextEvent event) {
event.getServletContext().removeAttribute(LOG4J_CONTEXT_ATTRIBUTE);
Configurator.shutdown();
}
+
+ private ClassLoader getClassLoader(ServletContext context) {
+ Method[] methods = context.getClass().getMethods();
+ Method getClassLoader = null;
+ for (Method method : methods) {
+ if (method.getName().equals("getClassLoader")) {
+ getClassLoader = method;
+ break;
+ }
+ }
+
+ if (getClassLoader != null) {
+ try {
+ return (ClassLoader) getClassLoader.invoke(context, null);
+ } catch (Exception ex) {
+ // Ignore the exception
+ }
+ }
+
+ return Log4jContextListener.class.getClassLoader();
+ }
}
Modified: logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/package-info.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/package-info.java?rev=1382381&r1=1382131&r2=1382381&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/package-info.java (original)
+++ logging/log4j/log4j2/trunk/web/src/main/java/org/apache/logging/log4j/core/web/package-info.java Sat Sep 8 22:23:25 2012
@@ -17,4 +17,4 @@
/**
* Classes to aid integration with Java EE.
*/
-package org.apache.logging.log4j.core.javaee;
+package org.apache.logging.log4j.core.web;
Added: logging/log4j/log4j2/trunk/web/src/site/site.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/web/src/site/site.xml?rev=1382381&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/web/src/site/site.xml (added)
+++ logging/log4j/log4j2/trunk/web/src/site/site.xml Sat Sep 8 22:23:25 2012
@@ -0,0 +1,38 @@
+<!--
+ 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.
+
+-->
+<project name="Log4j Web">
+ <bannerLeft>
+ <name>Logging Services</name>
+ <src>../images/ls-logo.jpg</src>
+ <href>../index.html</href>
+ </bannerLeft>
+ <bannerRight>
+ <src>../images/logo.jpg</src>
+ </bannerRight>
+ <body>
+ <links>
+ <item name="Apache" href="http://www.apache.org/" />
+ <item name="Logging Services" href="http://logging.apache.org/"/>
+ <item name="Log4j" href="../index.html"/>
+ </links>
+ <menu ref="reports"/>
+ <footer><div class="row span16">Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, and the
+ Apache Logging project logo are trademarks of The Apache Software Foundation.</div>
+ </footer>
+ </body>
+</project>
Added: 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=1382381&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/web/src/site/xdoc/index.xml (added)
+++ logging/log4j/log4j2/trunk/web/src/site/xdoc/index.xml Sat Sep 8 22:23:25 2012
@@ -0,0 +1,63 @@
+<?xml version="1.0"?>
+<!--
+ 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.
+-->
+
+<document>
+ <properties>
+ <title>Log4j Web</title>
+ <author email="rgoers@apache.org">Ralph Goers</author>
+ </properties>
+
+ <body>
+ <section name="Log4j Web">
+
+ <p>
+ Contains classes that can be used to initialize logging within a web application.
+ </p>
+
+ <subsection name="Log4jContextListener">
+ <p>Log4j provides a ServletContextListener that will initialize the LoggerContext for your web
+ application. It accepts two initialization parameters:
+ <ol>
+ <li>log4jContextName - the name to be given to the LoggerContext. If not specified this will
+ default to the display-name of the web application.</li>
+ <li>log4jConfiguration - the location of the Log4j configuration file. If the file protocol
+ is not specified then the file will be located both in the class path and on the file system.
+ If the location is a relative path the file location will be relative to the current
+ working directory of the running server.</li>
+ </ol>
+ </p>
+ </subsection>
+ <subsection name="JNDIContextFilter">
+ <p>The JNDIContextFilter is a Servlet Filter that can be used in conjunction with the JNDIContextSelector
+ to identify the LoggerContext that should be used. It accepts two parameters.</p>
+ <ol>
+ <li>context-name - the name to be given to the LoggerContext. A context name must be specified.</li>
+ <li>config-location - the location of the Log4j configuration file.</li>
+ </ol>
+ </subsection>
+
+ </section>
+
+ <section name="Requirements">
+ <p>
+ The Log4j web components require at least Java 5.
+ </p>
+ </section>
+
+ </body>
+</document>
\ No newline at end of file
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=1382381&r1=1382131&r2=1382381&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 Sat Sep 8 22:23:25 2012
@@ -14,7 +14,7 @@
* See the license for the specific language governing permissions and
* limitations under the license.
*/
-package org.apache.logging.log4j.core.javaee;
+package org.apache.logging.log4j.core.web;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;