You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ck...@apache.org on 2019/12/24 16:09:48 UTC

[logging-log4j2] branch release-2.x updated: LOG4J2-2739: Fix erroneous log4j-jul recursive logger detection

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

ckozak 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 e2b34f9  LOG4J2-2739: Fix erroneous log4j-jul recursive logger detection
e2b34f9 is described below

commit e2b34f9c5a6304b133ebad9d5373f7a67f444a84
Author: Carter Kozak <ck...@apache.org>
AuthorDate: Tue Dec 24 11:06:37 2019 -0500

    LOG4J2-2739: Fix erroneous log4j-jul recursive logger detection
    
    The new implementation takes the requested logger name into account.
    Invocations of jul LogManager.getLogger may become slightly less
    performant due to string hashing and allocaiton overhead, but in
    these scenarios it's best to reuse logger instances.
---
 .../org/apache/logging/log4j/jul/LogManager.java    | 21 ++++++++++++---------
 src/changes/changes.xml                             |  3 +++
 2 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LogManager.java b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LogManager.java
index f009401..6225b4c 100644
--- a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LogManager.java
+++ b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LogManager.java
@@ -18,9 +18,10 @@ package org.apache.logging.log4j.jul;
 
 import java.util.Collections;
 import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.logging.Logger;
 
-import com.sun.org.apache.xpath.internal.operations.Bool;
 import org.apache.logging.log4j.LoggingException;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.apache.logging.log4j.util.LoaderUtil;
@@ -42,7 +43,8 @@ public class LogManager extends java.util.logging.LogManager {
 
     private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
     private final AbstractLoggerAdapter loggerAdapter;
-    private final ThreadLocal<Boolean> recursive = ThreadLocal.withInitial(() -> Boolean.FALSE);
+    // Contains the set of logger names that are actively being requested using getLogger.
+    private final ThreadLocal<Set<String>> recursive = ThreadLocal.withInitial(HashSet::new);
 
     public LogManager() {
         super();
@@ -88,16 +90,17 @@ public class LogManager extends java.util.logging.LogManager {
     @Override
     public Logger getLogger(final String name) {
         LOGGER.trace("Call to LogManager.getLogger({})", name);
-        if (recursive.get()) {
+        Set<String> activeRequests = recursive.get();
+        if (activeRequests.add(name)) {
+            try {
+                return loggerAdapter.getLogger(name);
+            } finally {
+                activeRequests.remove(name);
+            }
+        } else {
             LOGGER.warn("Recursive call to getLogger for {} ignored.", name);
             return new NoOpLogger(name);
         }
-        recursive.set(Boolean.TRUE);
-        try {
-            return loggerAdapter.getLogger(name);
-        } finally {
-            recursive.set(Boolean.FALSE);
-        }
     }
 
     @Override
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9e19be4..569555a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -45,6 +45,9 @@
       <action issue="LOG4J2-2747" dev="ckozak" type="fix">
         Fix a memory leak using fully asynchronous logging when the queue is full using the 'discard' asynchronous queue full strategy.
       </action>
+      <action issue="LOG4J2-2739" dev="ckozak" type="fix">
+        Fix erroneous log4j-jul recursive logger detection resulting in some no-op JUL loggers and 'WARN Recursive call to getLogger' being reported by the status logger.
+      </action>
     </release>
     <release version="2.13.0" date="2019-12-11" description="GA Release 2.13.0">
       <action issue="LOG4J2-2058" dev="rgoers" type="fix">