ppkarwasz commented on code in PR #2374:
URL: https://github.com/apache/logging-log4j2/pull/2374#discussion_r1527193084


##########
log4j-api/src/main/java/org/apache/logging/log4j/spi/Provider.java:
##########
@@ -97,127 +217,266 @@ public String getVersions() {
 
     /**
      * Gets the priority (natural ordering) of this Provider.
-     *
+     * <p>
+     *     Log4j selects the highest priority provider.
+     * </p>
      * @return the priority of this Provider
      */
     public Integer getPriority() {
         return priority;
     }
 
     /**
-     * Gets the class name of the {@link 
org.apache.logging.log4j.spi.LoggerContextFactory} implementation of this
-     * Provider.
+     * Gets the class name of the {@link LoggerContextFactory} implementation 
of this Provider.
      *
-     * @return the class name of a LoggerContextFactory implementation
+     * @return the class name of a LoggerContextFactory implementation or 
{@code null} if unspecified.
+     * @see #loadLoggerContextFactory()
      */
     public String getClassName() {
-        if (loggerContextFactoryClass != null) {
-            return loggerContextFactoryClass.getName();
-        }
-        return className;
+        return loggerContextFactoryClass != null ? 
loggerContextFactoryClass.getName() : className;
     }
 
     /**
-     * Loads the {@link org.apache.logging.log4j.spi.LoggerContextFactory} 
class specified by this Provider.
+     * Loads the {@link LoggerContextFactory} class specified by this Provider.
      *
-     * @return the LoggerContextFactory implementation class or {@code null} 
if there was an error loading it
+     * @return the LoggerContextFactory implementation class or {@code null} 
if unspecified or a loader error occurred.
+     * @see #createLoggerContextFactory()
      */
     public Class<? extends LoggerContextFactory> loadLoggerContextFactory() {
         if (loggerContextFactoryClass != null) {
             return loggerContextFactoryClass;
         }
-        if (className == null) {
-            return null;
-        }
+        final String className = getClassName();
         final ClassLoader loader = classLoader.get();
-        if (loader == null) {
+        // Support for deprecated {@code META-INF/log4j-provider.properties} 
format.
+        // In the remaining cases {@code loader == null}.
+        if (loader == null || className == null) {
             return null;
         }
         try {
             final Class<?> clazz = loader.loadClass(className);
             if (LoggerContextFactory.class.isAssignableFrom(clazz)) {
                 return clazz.asSubclass(LoggerContextFactory.class);
+            } else {
+                LOGGER.error(
+                        "Class {} specified in {} does not extend {}",
+                        className,
+                        getUrl(),
+                        LoggerContextFactory.class.getName());
             }
         } catch (final Exception e) {
-            LOGGER.error("Unable to create class {} specified in {}", 
className, url.toString(), e);
+            LOGGER.error("Unable to create class {} specified in {}", 
className, getUrl(), e);
         }
         return null;
     }
 
     /**
-     * Gets the class name of the {@link 
org.apache.logging.log4j.spi.ThreadContextMap} implementation of this Provider.
-     *
+     * Extension point for providers to create a {@link LoggerContextFactory}.
+     * @since 2.24.0
+     */
+    protected LoggerContextFactory createLoggerContextFactory() {
+        final Class<? extends LoggerContextFactory> factoryClass = 
loadLoggerContextFactory();
+        if (factoryClass != null) {
+            try {
+                return LoaderUtil.newInstanceOf(factoryClass);
+            } catch (final Exception e) {
+                LOGGER.error(
+                        "Unable to create instance of class {} specified in 
{}", factoryClass.getName(), getUrl(), e);
+            }
+        }
+        LOGGER.warn("Falling back to {}", SimpleLoggerContextFactory.INSTANCE);
+        return SimpleLoggerContextFactory.INSTANCE;
+    }
+
+    /**
+     * @return A lazily initialized logger context factory
+     * @since 2.24.0
+     */
+    public final LoggerContextFactory getLoggerContextFactory() {

Review Comment:
   I wanted to stress out the `LoggerContextFactory` is a singleton.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to