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

rgoers 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 5ffbbee76b LOG4J2-3589 - Avoid storing strong references to the 
LoggerContext
5ffbbee76b is described below

commit 5ffbbee76be9f7f701e835e0e99756f978ff7fbd
Author: Ralph Goers <[email protected]>
AuthorDate: Thu Sep 8 20:30:30 2022 -0700

    LOG4J2-3589 - Avoid storing strong references to the LoggerContext
---
 .../lookup/AbstractLoggerContextAwareLookup.java   | 35 ----------------------
 .../logging/log4j/core/lookup/Interpolator.java    |  7 +++--
 .../logging/log4j/spring/boot/SpringLookup.java    |  5 ++--
 3 files changed, 6 insertions(+), 41 deletions(-)

diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/AbstractLoggerContextAwareLookup.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/AbstractLoggerContextAwareLookup.java
deleted file mode 100644
index bec6b4cc9f..0000000000
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/AbstractLoggerContextAwareLookup.java
+++ /dev/null
@@ -1,35 +0,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.
- */
-
-package org.apache.logging.log4j.core.lookup;
-
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.config.LoggerContextAware;
-
-/**
- * StrLookup that is ConfigurationAware. Handles saving the Configuration.
- *
- * @since 2.6
- */
-public abstract class AbstractLoggerContextAwareLookup extends AbstractLookup 
implements LoggerContextAware {
-    protected LoggerContext loggerContext;
-
-    @Override
-    public void setLoggerContext(final LoggerContext loggerContext) {
-        this.loggerContext = loggerContext;
-    }
-}
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
index 0660b61979..f815836d1e 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
@@ -16,6 +16,7 @@
  */
 package org.apache.logging.log4j.core.lookup;
 
+import java.lang.ref.WeakReference;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -59,7 +60,7 @@ public class Interpolator extends 
AbstractConfigurationAwareLookup implements Lo
 
     private final StrLookup defaultLookup;
 
-    protected LoggerContext loggerContext;
+    protected WeakReference<LoggerContext> loggerContext;
 
     public Interpolator(final StrLookup defaultLookup) {
         this(defaultLookup, null);
@@ -190,7 +191,7 @@ public class Interpolator extends 
AbstractConfigurationAwareLookup implements Lo
                 ((ConfigurationAware) lookup).setConfiguration(configuration);
             }
             if (lookup instanceof LoggerContextAware) {
-                ((LoggerContextAware) lookup).setLoggerContext(loggerContext);
+                ((LoggerContextAware) 
lookup).setLoggerContext(loggerContext.get());
             }
             LookupResult value = null;
             if (lookup != null) {
@@ -213,7 +214,7 @@ public class Interpolator extends 
AbstractConfigurationAwareLookup implements Lo
         if (loggerContext == null) {
             return;
         }
-        this.loggerContext = loggerContext;
+        this.loggerContext = new WeakReference<>(loggerContext);
     }
 
     @Override
diff --git 
a/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringLookup.java
 
b/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringLookup.java
index 7bbe26b892..3d3821094c 100644
--- 
a/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringLookup.java
+++ 
b/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringLookup.java
@@ -22,8 +22,8 @@ import java.util.regex.Pattern;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.LoggerContextAware;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
-import org.apache.logging.log4j.core.lookup.AbstractLoggerContextAwareLookup;
 import org.apache.logging.log4j.core.lookup.StrLookup;
 import org.apache.logging.log4j.core.util.Integers;
 import org.apache.logging.log4j.status.StatusLogger;
@@ -33,7 +33,7 @@ import org.springframework.core.env.Environment;
  * Lookup for Spring properties.
  */
 @Plugin(name = "spring", category = StrLookup.CATEGORY)
-public class SpringLookup extends AbstractLoggerContextAwareLookup implements 
StrLookup {
+public class SpringLookup implements LoggerContextAware, StrLookup {
 
     private static final Logger LOGGER = StatusLogger.getLogger();
     private static final String ACTIVE = "profiles.active";
@@ -118,7 +118,6 @@ public class SpringLookup extends 
AbstractLoggerContextAwareLookup implements St
     @Override
     public void setLoggerContext(final LoggerContext loggerContext) {
         if (loggerContext != null) {
-            super.setLoggerContext(loggerContext);
             environment = (Environment) 
loggerContext.getObject(Log4j2SpringBootLoggingSystem.ENVIRONMENT_KEY);
         } else {
             LOGGER.warn("Attempt to set LoggerContext reference to null in 
SpringLookup");

Reply via email to