This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new be8af7f32b [LOG4J2-2557] Remove recursion between Logger and 
LoggerRepository
be8af7f32b is described below

commit be8af7f32b53401ccbda80f3245ad21121b0439f
Author: Piotr P. Karwasz <[email protected]>
AuthorDate: Thu Jul 28 07:43:05 2022 +0200

    [LOG4J2-2557] Remove recursion between Logger and LoggerRepository
    
    The `Category` class constructor should not use the static method
    `LogManager.getLoggerRepository()` to find the `LoggerRepository` that
    created it. The value should be injected.
---
 .../src/main/java/org/apache/log4j/Category.java   |  12 +--
 .../src/main/java/org/apache/log4j/Hierarchy.java  | 103 +++------------------
 .../test/java/org/apache/log4j/CategoryTest.java   |   1 +
 3 files changed, 20 insertions(+), 96 deletions(-)

diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/Category.java 
b/log4j-1.2-api/src/main/java/org/apache/log4j/Category.java
index 0ebffc9c77..60e3086dc8 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/Category.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/Category.java
@@ -37,7 +37,6 @@ import org.apache.log4j.spi.AppenderAttachable;
 import org.apache.log4j.spi.HierarchyEventListener;
 import org.apache.log4j.spi.LoggerRepository;
 import org.apache.log4j.spi.LoggingEvent;
-import org.apache.logging.log4j.core.config.LoggerConfig;
 import org.apache.logging.log4j.message.LocalizedMessage;
 import org.apache.logging.log4j.message.MapMessage;
 import org.apache.logging.log4j.message.Message;
@@ -174,13 +173,10 @@ public class Category implements AppenderAttachable {
     protected Category(final LoggerContext context, final String name) {
         this.name = name;
         this.logger = context.getLogger(name);
-        this.repository = LogManager.getLoggerRepository();
-        // this.rendererMap = ((RendererSupport) repository).getRendererMap();
     }
 
     Category(final org.apache.logging.log4j.Logger logger) {
         this.logger = logger;
-        // rendererMap = ((RendererSupport) 
LogManager.getLoggerRepository()).getRendererMap();
     }
 
     /**
@@ -475,8 +471,12 @@ public class Category implements AppenderAttachable {
             return null;
         }
         final ConcurrentMap<String, Logger> loggers = 
Hierarchy.getLoggersMap(loggerContext);
-        final Logger parentLogger = loggers.get(parent.getName());
-        return parentLogger == null ? new Category(parent) : parentLogger;
+        Category parentLogger = loggers.get(parent.getName());
+        if (parentLogger == null) {
+            parentLogger = new Category(parent);
+            parentLogger.setHierarchy(getLoggerRepository());
+        }
+        return parentLogger;
     }
 
     public final Level getPriority() {
diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/Hierarchy.java 
b/log4j-1.2-api/src/main/java/org/apache/log4j/Hierarchy.java
index 2fe2d79925..203a680961 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/Hierarchy.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/Hierarchy.java
@@ -99,16 +99,24 @@ public class Hierarchy implements LoggerRepository, 
RendererSupport, ThrowableRe
         return PrivateLogManager.getContext();
     }
 
-    static Logger getInstance(final LoggerContext context, final String name) {
+    private Logger getInstance(final LoggerContext context, final String name) 
{
         return getInstance(context, name, LOGGER_ADAPTER);
     }
 
-    static Logger getInstance(final LoggerContext context, final String name, 
final LoggerFactory factory) {
-        return getLoggersMap(context).computeIfAbsent(name, k -> 
factory.makeNewLoggerInstance(name));
+    private Logger getInstance(final LoggerContext context, final String name, 
final LoggerFactory factory) {
+        return getLoggersMap(context).computeIfAbsent(name, k -> {
+            final Logger logger = factory.makeNewLoggerInstance(name);
+            logger.setHierarchy(this);
+            return logger;
+        });
     }
 
-    static Logger getInstance(final LoggerContext context, final String name, 
final PrivateLoggerAdapter factory) {
-        return getLoggersMap(context).computeIfAbsent(name, k -> 
factory.newLogger(name, context));
+    private Logger getInstance(final LoggerContext context, final String name, 
final PrivateLoggerAdapter factory) {
+        return getLoggersMap(context).computeIfAbsent(name, k -> {
+            final Logger logger = factory.newLogger(name, context);
+            logger.setHierarchy(this);
+            return logger;
+        });
     }
 
     static ConcurrentMap<String, Logger> getLoggersMap(final LoggerContext 
context) {
@@ -117,10 +125,6 @@ public class Hierarchy implements LoggerRepository, 
RendererSupport, ThrowableRe
         }
     }
 
-    static Logger getRootLogger(final LoggerContext context) {
-        return getInstance(context, 
org.apache.logging.log4j.LogManager.ROOT_LOGGER_NAME);
-    }
-
     private final LoggerFactory defaultFactory;
     private final Vector listeners;
     Hashtable ht;
@@ -527,85 +531,4 @@ public class Hierarchy implements LoggerRepository, 
RendererSupport, ThrowableRe
           }
     }
 
-    /**
-     * We update the links for all the children that placed themselves in the 
provision node 'pn'. The second argument 'cat'
-     * is a reference for the newly created Logger, parent of all the children 
in 'pn'
-     *
-     * We loop on all the children 'c' in 'pn':
-     *
-     * If the child 'c' has been already linked to a child of 'cat' then there 
is no need to update 'c'.
-     *
-     * Otherwise, we set cat's parent field to c's parent and set c's parent 
field to cat.
-     *
-     */
-    final private void updateChildren(final ProvisionNode pn, final Logger 
logger) {
-        // System.out.println("updateChildren called for " + logger.name);
-        final int last = pn.size();
-
-        for (int i = 0; i < last; i++) {
-            final Logger l = (Logger) pn.elementAt(i);
-            // System.out.println("Updating child " +p.name);
-
-            // Unless this child already points to a correct (lower) parent,
-            // make cat.parent point to l.parent and l.parent to cat.
-            if (!l.parent.name.startsWith(logger.name)) {
-                logger.parent = l.parent;
-                l.parent = logger;
-            }
-        }
-    }
-
-    /**
-     * This method loops through all the *potential* parents of 'cat'. There 3 
possible cases:
-     *
-     * 1) No entry for the potential parent of 'cat' exists
-     *
-     * We create a ProvisionNode for this potential parent and insert 'cat' in 
that provision node.
-     *
-     * 2) There entry is of type Logger for the potential parent.
-     *
-     * The entry is 'cat's nearest existing parent. We update cat's parent 
field with this entry. We also break from the
-     * loop because updating our parent's parent is our parent's 
responsibility.
-     *
-     * 3) There entry is of type ProvisionNode for this potential parent.
-     *
-     * We add 'cat' to the list of children for this potential parent.
-     */
-    final private void updateParents(final Logger cat) {
-        final String name = cat.name;
-        final int length = name.length();
-        boolean parentFound = false;
-
-        // System.out.println("UpdateParents called for " + name);
-
-        // if name = "w.x.y.z", loop thourgh "w.x.y", "w.x" and "w", but not 
"w.x.y.z"
-        for (int i = name.lastIndexOf('.', length - 1); i >= 0; i = 
name.lastIndexOf('.', i - 1)) {
-            final String substr = name.substring(0, i);
-
-            // System.out.println("Updating parent : " + substr);
-            final CategoryKey key = new CategoryKey(substr); // simple 
constructor
-            final Object o = ht.get(key);
-            // Create a provision node for a future parent.
-            if (o == null) {
-                // System.out.println("No parent "+substr+" found. Creating 
ProvisionNode.");
-                final ProvisionNode pn = new ProvisionNode(cat);
-                ht.put(key, pn);
-            } else if (o instanceof Category) {
-                parentFound = true;
-                cat.parent = (Category) o;
-                // System.out.println("Linking " + cat.name + " -> " + 
((Category) o).name);
-                break; // no need to update the ancestors of the closest 
ancestor
-            } else if (o instanceof ProvisionNode) {
-                ((ProvisionNode) o).addElement(cat);
-            } else {
-                final Exception e = new IllegalStateException("unexpected 
object type " + o.getClass() + " in ht.");
-                e.printStackTrace();
-            }
-        }
-        // If we could not find any existing parents, then link with root.
-        if (!parentFound) {
-            cat.parent = root;
-        }
-    }
-
 }
diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/CategoryTest.java 
b/log4j-1.2-api/src/test/java/org/apache/log4j/CategoryTest.java
index a1b8334f4c..2ed9b2fcbd 100644
--- a/log4j-1.2-api/src/test/java/org/apache/log4j/CategoryTest.java
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/CategoryTest.java
@@ -94,6 +94,7 @@ public class CategoryTest {
     public void testForcedLog() {
         final MockCategory category = new MockCategory("org.example.foo");
         category.setAdditivity(false);
+        category.setHierarchy(LogManager.getHierarchy());
         ((org.apache.logging.log4j.core.Logger) 
category.getLogger()).addAppender(appender);
         // Logging a String
         category.info("Hello, World");

Reply via email to