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

rgoers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new 70de14fcc1 LOG4J2-3589 - Avoid storing strong references to the 
LoggerContext
70de14fcc1 is described below

commit 70de14fcc1dfb74173846bf93e8c90a0bb275fc7
Author: Piotr P. Karwasz <[email protected]>
AuthorDate: Sun Sep 4 07:04:59 2022 +0200

    LOG4J2-3589 - Avoid storing strong references to the LoggerContext
---
 .../apache/logging/log4j/core/LoggerContext.java   | 11 ++---
 .../log4j/core/config/AbstractConfiguration.java   |  2 +-
 .../plugins/visit/PluginLoggerContextVisitor.java  |  7 ++-
 .../core/filter/MutableThreadContextMapFilter.java |  5 +--
 .../lookup/AbstractLoggerContextAwareLookup.java   | 35 ---------------
 .../logging/log4j/core/lookup/Interpolator.java    |  7 +--
 .../log4j/spring/boot/SpringEnvironmentHolder.java | 50 ----------------------
 .../logging/log4j/spring/boot/SpringLookup.java    |  8 ++--
 .../log4j/spring/boot/SpringPropertySource.java    |  2 +-
 9 files changed, 20 insertions(+), 107 deletions(-)

diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
index 62d30eb666..ddc22eefc9 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
@@ -46,6 +46,7 @@ import org.apache.logging.log4j.util.PropertiesUtil;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.io.File;
+import java.lang.ref.WeakReference;
 import java.net.URI;
 import java.util.Collection;
 import java.util.List;
@@ -72,7 +73,7 @@ public class LoggerContext extends AbstractLifeCycle
      * Property name of the property change event fired if the configuration 
is changed.
      */
     public static final String PROPERTY_CONFIG = "config";
-    public  static final Key<LoggerContext> KEY = new Key<>() {};
+    public  static final Key<WeakReference<LoggerContext>> KEY = new Key<>() 
{};
 
     private static final Configuration NULL_CONFIGURATION = new 
NullConfiguration();
 
@@ -123,7 +124,7 @@ public class LoggerContext extends AbstractLifeCycle
     public LoggerContext(final String name, final Object externalContext, 
final URI configLocn) {
         this(name, externalContext, configLocn, DI.createInjector());
         injector.init();
-        injector.registerBindingIfAbsent(KEY, () -> this);
+        injector.registerBindingIfAbsent(KEY, () -> new WeakReference<>(this));
     }
 
     /**
@@ -141,7 +142,7 @@ public class LoggerContext extends AbstractLifeCycle
         }
         this.configLocation = configLocn;
         this.injector = injector.copy();
-        injector.registerBindingIfAbsent(KEY, () -> this);
+        injector.registerBindingIfAbsent(KEY, () -> new WeakReference<>(this));
     }
 
     /**
@@ -155,7 +156,7 @@ public class LoggerContext extends AbstractLifeCycle
     public LoggerContext(final String name, final Object externalContext, 
final String configLocn) {
         this(name, externalContext, configLocn, DI.createInjector());
         injector.init();
-        injector.registerBindingIfAbsent(KEY, () -> this);
+        injector.registerBindingIfAbsent(KEY, () -> new WeakReference<>(this));
     }
 
     /**
@@ -184,7 +185,7 @@ public class LoggerContext extends AbstractLifeCycle
             configLocation = null;
         }
         this.injector = injector.copy();
-        this.injector.registerBindingIfAbsent(KEY, () -> this);
+        this.injector.registerBindingIfAbsent(KEY, () -> new 
WeakReference<>(this));
     }
 
     public void addShutdownListener(final LoggerContextShutdownAware listener) 
{
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java
index a42cf31263..8f604e7f18 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java
@@ -642,7 +642,7 @@ public abstract class AbstractConfiguration extends 
AbstractFilterable implement
 
     protected void doConfigure() {
         injector.registerBinding(Keys.SUBSTITUTOR_KEY, () -> 
configurationStrSubstitutor::replace);
-        injector.registerBinding(LoggerContext.KEY, () -> loggerContext.get());
+        injector.registerBinding(LoggerContext.KEY, () -> loggerContext);
         processConditionals(rootNode);
         preConfigure(rootNode);
         configurationScheduler.start();
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visit/PluginLoggerContextVisitor.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visit/PluginLoggerContextVisitor.java
index 3306544328..0b699f6833 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visit/PluginLoggerContextVisitor.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visit/PluginLoggerContextVisitor.java
@@ -17,13 +17,12 @@
 
 package org.apache.logging.log4j.core.config.plugins.visit;
 
+import java.lang.ref.WeakReference;
 import java.lang.reflect.Field;
 import java.lang.reflect.Parameter;
 
 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.Configuration;
 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
 import org.apache.logging.log4j.plugins.Inject;
 import org.apache.logging.log4j.plugins.Node;
@@ -40,8 +39,8 @@ public class PluginLoggerContextVisitor implements 
NodeVisitor {
     private LoggerContext loggerContext;
 
     @Inject
-    public PluginLoggerContextVisitor(final LoggerContext loggerContext) {
-        this.loggerContext = loggerContext;
+    public PluginLoggerContextVisitor(final WeakReference<LoggerContext> 
loggerContext) {
+        this.loggerContext = loggerContext.get();
     }
 
     @Override
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MutableThreadContextMapFilter.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MutableThreadContextMapFilter.java
index 90e28d5363..e2266f84d9 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MutableThreadContextMapFilter.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MutableThreadContextMapFilter.java
@@ -377,14 +377,14 @@ public class MutableThreadContextMapFilter extends 
AbstractFilter {
                             configResult.pairs = pairs.toArray(EMPTY_ARRAY);
                             configResult.status = Status.SUCCESS;
                         } else {
-                            LOGGER.debug("No configuration data in {}", 
source.toString());
                             configResult.status = Status.EMPTY;
                         }
                     } else {
+                        LOGGER.debug("No configuration data in {}", 
source.toString());
                         configResult.status = Status.EMPTY;
                     }
                 } else {
-                    LOGGER.warn("No config element in 
MutableThreadContextMapFilter configuration");
+                    LOGGER.warn("No configs element in 
MutableThreadContextMapFilter configuration");
                     configResult.status = Status.ERROR;
                 }
             } catch (Exception ex) {
@@ -392,7 +392,6 @@ public class MutableThreadContextMapFilter extends 
AbstractFilter {
                 configResult.status = Status.ERROR;
             }
         } else {
-            LOGGER.warn("No configs element in MutableThreadContextMapFilter 
configuration");
             configResult.status = result.getStatus();
         }
         return configResult;
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 647b6ecb40..bef28b7237 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
@@ -27,6 +27,7 @@ import org.apache.logging.log4j.plugins.di.Injector;
 import org.apache.logging.log4j.plugins.di.Keys;
 import org.apache.logging.log4j.status.StatusLogger;
 
+import java.lang.ref.WeakReference;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -60,7 +61,7 @@ public class Interpolator extends 
AbstractConfigurationAwareLookup implements Lo
 
     private final StrLookup defaultLookup;
 
-    private LoggerContext loggerContext = null;
+    private WeakReference<LoggerContext> loggerContext = null;
 
     // Used by tests
     public Interpolator(final StrLookup defaultLookup) {
@@ -186,7 +187,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());
                 }
                 value = event == null ? lookup.lookup(name) : 
lookup.lookup(event, name);
             }
@@ -206,7 +207,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/SpringEnvironmentHolder.java
 
b/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringEnvironmentHolder.java
deleted file mode 100644
index 4091ef6aa3..0000000000
--- 
a/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringEnvironmentHolder.java
+++ /dev/null
@@ -1,50 +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.spring.boot;
-
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
-
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.internal.LogManagerStatus;
-import org.springframework.core.env.Environment;
-
-/**
- * Provides access to the Spring Environment.
- */
-public class SpringEnvironmentHolder {
-
-    private volatile Environment environment;
-    private Lock lock = new ReentrantLock();
-
-
-    protected Environment getEnvironment() {
-        if (environment == null && LogManagerStatus.isInitialized() && 
LogManager.getFactory() != null &&
-                
LogManager.getFactory().hasContext(SpringEnvironmentHolder.class.getName(), 
null, false)) {
-            lock.lock();
-            try {
-                if (environment == null) {
-                    Object obj = 
LogManager.getContext(false).getObject(Log4j2SpringBootLoggingSystem.ENVIRONMENT_KEY);
-                    environment = obj instanceof Environment ? (Environment) 
obj : null;
-                }
-            } finally {
-                lock.unlock();
-            }
-        }
-        return environment;
-    }
-}
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 69e9f41cfb..b5d1b673ec 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
@@ -19,10 +19,9 @@ package org.apache.logging.log4j.spring.boot;
 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.lookup.AbstractLoggerContextAwareLookup;
-import org.apache.logging.log4j.core.lookup.StrLookup;
+import org.apache.logging.log4j.core.config.LoggerContextAware;
 import org.apache.logging.log4j.core.lookup.Lookup;
-import org.apache.logging.log4j.plugins.Inject;
+import org.apache.logging.log4j.core.lookup.StrLookup;
 import org.apache.logging.log4j.plugins.Plugin;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.springframework.core.env.Environment;
@@ -35,7 +34,7 @@ import java.util.regex.Pattern;
  */
 @Lookup
 @Plugin("spring")
-public class SpringLookup extends AbstractLoggerContextAwareLookup {
+public class SpringLookup implements LoggerContextAware, StrLookup {
 
     private static final Logger LOGGER = StatusLogger.getLogger();
     private static final String ACTIVE = "profiles.active";
@@ -138,7 +137,6 @@ public class SpringLookup extends 
AbstractLoggerContextAwareLookup {
     @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");
diff --git 
a/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringPropertySource.java
 
b/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringPropertySource.java
index 23ad07060d..d5279be696 100644
--- 
a/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringPropertySource.java
+++ 
b/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringPropertySource.java
@@ -22,7 +22,7 @@ import org.springframework.core.env.Environment;
 /**
  * Returns properties from Spring.
  */
-public class SpringPropertySource extends SpringEnvironmentHolder implements 
PropertySource {
+public class SpringPropertySource implements PropertySource {
 
     private static final int DEFAULT_PRIORITY = -100;
 

Reply via email to