logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattsic...@apache.org
Subject svn commit: r1611999 - in /logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j: LogManager.java ThreadContext.java
Date Sun, 20 Jul 2014 02:13:21 GMT
Author: mattsicker
Date: Sun Jul 20 02:13:21 2014
New Revision: 1611999

URL: http://svn.apache.org/r1611999
Log:
Use the new methods in Provider for loading classes.

Modified:
    logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java
    logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java

Modified: logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java?rev=1611999&r1=1611998&r2=1611999&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java
Sun Jul 20 02:13:21 2014
@@ -28,6 +28,7 @@ import org.apache.logging.log4j.spi.Logg
 import org.apache.logging.log4j.spi.LoggerContextFactory;
 import org.apache.logging.log4j.spi.Provider;
 import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.LoaderUtil;
 import org.apache.logging.log4j.util.PropertiesUtil;
 import org.apache.logging.log4j.util.ProviderUtil;
 import org.apache.logging.log4j.util.Strings;
@@ -39,7 +40,11 @@ public class LogManager {
 
     private static volatile LoggerContextFactory factory;
 
-    private static final String FACTORY_PROPERTY_NAME = "log4j2.loggerContextFactory";
+    /**
+     * Log4j property to set to the fully qualified class name of a custom implementation
of
+     * {@link org.apache.logging.log4j.spi.LoggerContextFactory}.
+     */
+    public static final String FACTORY_PROPERTY_NAME = "log4j2.loggerContextFactory";
 
     private static final Logger LOGGER = StatusLogger.getLogger();
 
@@ -55,18 +60,18 @@ public class LogManager {
     static {
         // Shortcut binding to force a specific logging implementation.
         final PropertiesUtil managerProps = PropertiesUtil.getProperties();
-        final String factoryClass = managerProps.getStringProperty(FACTORY_PROPERTY_NAME);
-        final ClassLoader cl = ProviderUtil.findClassLoader();
-        if (factoryClass != null) {
+        final String factoryClassName = managerProps.getStringProperty(FACTORY_PROPERTY_NAME);
+        final ClassLoader cl = LoaderUtil.getThreadContextClassLoader();
+        if (factoryClassName != null) {
             try {
-                final Class<?> clazz = cl.loadClass(factoryClass);
+                final Class<?> clazz = cl.loadClass(factoryClassName);
                 if (LoggerContextFactory.class.isAssignableFrom(clazz)) {
                     factory = (LoggerContextFactory) clazz.newInstance();
                 }
             } catch (final ClassNotFoundException cnfe) {
-                LOGGER.error("Unable to locate configured LoggerContextFactory {}", factoryClass);
+                LOGGER.error("Unable to locate configured LoggerContextFactory {}", factoryClassName);
             } catch (final Exception ex) {
-                LOGGER.error("Unable to create configured LoggerContextFactory {}", factoryClass,
ex);
+                LOGGER.error("Unable to create configured LoggerContextFactory {}", factoryClassName,
ex);
             }
         }
 
@@ -75,29 +80,18 @@ public class LogManager {
 
             if (ProviderUtil.hasProviders()) {
                 for (final Provider provider : ProviderUtil.getProviders()) {
-                    final String className = provider.getClassName();
-                    if (className != null) {
+                    final Class<? extends LoggerContextFactory> factoryClass = provider.loadLoggerContextFactory();
+                    if (factoryClass != null) {
                         try {
-                            final Class<?> clazz = cl.loadClass(className);
-                            if (LoggerContextFactory.class.isAssignableFrom(clazz)) {
-                                factories.put(provider.getPriority(), (LoggerContextFactory)
clazz.newInstance());
-                            } else {
-                                LOGGER.error("{} does not implement {}", className, LoggerContextFactory.class.getName());
-                            }
-                        } catch (final ClassNotFoundException cnfe) {
-                            LOGGER.error("Unable to locate class {} specified in {}", className,
-                                provider.getUrl().toString(), cnfe);
-                        } catch (final IllegalAccessException iae) {
-                            LOGGER.error("Unable to create class {} specified in {}", className,
-                                provider.getUrl().toString(), iae);
+                            factories.put(provider.getPriority(), factoryClass.newInstance());
                         } catch (final Exception e) {
-                            LOGGER.error("Unable to create class {} specified in {}", className,
+                            LOGGER.error("Unable to create class {} specified in {}", factoryClass.getName(),
                                 provider.getUrl().toString(), e);
                         }
                     }
                 }
 
-                if (factories.size() == 0) {
+                if (factories.isEmpty()) {
                     LOGGER.error("Unable to locate a logging implementation, using SimpleLogger");
                     factory = new SimpleLoggerContextFactory();
                 } else {
@@ -145,6 +139,7 @@ public class LogManager {
      * <p>
      * WARNING - The LoggerContext returned by this method may not be the LoggerContext used
to create a Logger
      * for the calling class.
+     * </p>
      * @return  The current LoggerContext.
      */
     public static LoggerContext getContext() {

Modified: logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java?rev=1611999&r1=1611998&r2=1611999&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
Sun Jul 20 02:13:21 2014
@@ -29,7 +29,6 @@ import java.util.NoSuchElementException;
 import org.apache.logging.log4j.message.ParameterizedMessage;
 import org.apache.logging.log4j.spi.DefaultThreadContextMap;
 import org.apache.logging.log4j.spi.DefaultThreadContextStack;
-import org.apache.logging.log4j.spi.LoggerContextFactory;
 import org.apache.logging.log4j.spi.Provider;
 import org.apache.logging.log4j.spi.ThreadContextMap;
 import org.apache.logging.log4j.spi.ThreadContextStack;
@@ -220,29 +219,25 @@ public final class ThreadContext  {
                     contextMap = (ThreadContextMap) clazz.newInstance();
                 }
             } catch (final ClassNotFoundException cnfe) {
-                LOGGER.error("Unable to locate configured LoggerContextFactory {}", threadContextMapName);
+                LOGGER.error("Unable to locate configured ThreadContextMap {}", threadContextMapName);
             } catch (final Exception ex) {
-                LOGGER.error("Unable to create configured LoggerContextFactory {}", threadContextMapName,
ex);
+                LOGGER.error("Unable to create configured ThreadContextMap {}", threadContextMapName,
ex);
             }
         }
         if (contextMap == null && ProviderUtil.hasProviders()) {
-            final LoggerContextFactory factory = LogManager.getFactory();
+            final String factoryClassName = LogManager.getFactory().getClass().getName();
             for (final Provider provider : ProviderUtil.getProviders()) {
-                threadContextMapName = provider.getThreadContextMap();
-                final String factoryClassName = provider.getClassName();
-                if (threadContextMapName != null && factory.getClass().getName().equals(factoryClassName))
{
-                    try {
-                        final Class<?> clazz = cl.loadClass(threadContextMapName);
-                        if (ThreadContextMap.class.isAssignableFrom(clazz)) {
-                            contextMap = (ThreadContextMap) clazz.newInstance();
+                if (factoryClassName.equals(provider.getClassName())) {
+                    final Class<? extends ThreadContextMap> clazz = provider.loadThreadContextMap();
+                    if (clazz != null) {
+                        try {
+                            contextMap = clazz.newInstance();
                             break;
+                        } catch (final Exception e) {
+                            LOGGER.error("Unable to locate or load configured ThreadContextMap
{}",
+                                provider.getThreadContextMap(), e);
+                            contextMap = new DefaultThreadContextMap(useMap);
                         }
-                    } catch (final ClassNotFoundException cnfe) {
-                        LOGGER.error("Unable to locate configured LoggerContextFactory {}",
threadContextMapName);
-                        contextMap = new DefaultThreadContextMap(useMap);
-                    } catch (final Exception ex) {
-                        LOGGER.error("Unable to create configured LoggerContextFactory {}",
threadContextMapName, ex);
-                        contextMap = new DefaultThreadContextMap(useMap);
                     }
                 }
             }



Mime
View raw message