You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2022/09/06 19:30:19 UTC

[logging-log4j2] 01/02: LOG4J2-3588 - Allow a PropertySource to be added

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

commit 383f64afc8eb9ba55425cd314792ecc9251e3046
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Mon Sep 5 12:42:00 2022 -0700

    LOG4J2-3588 - Allow a PropertySource to be added
---
 .../apache/logging/log4j/util/PropertiesUtil.java   | 21 ++++++++++++++++++++-
 .../spring/boot/Log4j2SpringBootLoggingSystem.java  |  5 ++++-
 .../log4j/spring/boot/SpringPropertySource.java     |  8 ++++++--
 .../org.apache.logging.log4j.util.PropertySource    | 15 ---------------
 ....java => Log4j2SpringBootLoggingSystemTest.java} |  2 +-
 src/changes/changes.xml                             |  3 +++
 6 files changed, 34 insertions(+), 20 deletions(-)

diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
index f03cfaf551..699de74250 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
@@ -35,6 +35,7 @@ import java.util.ServiceLoader;
 import java.util.Set;
 import java.util.TreeSet;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentSkipListSet;
 
 /**
  * <em>Consider this class private.</em>
@@ -123,6 +124,16 @@ public final class PropertiesUtil {
         return COMPONENT_PROPERTIES.get();
     }
 
+    /**
+     * Allows a PropertySource to be added after PropertiesUtil has been created.
+     * @param propertySource the PropertySource to add.
+     */
+    public void addPropertySource(PropertySource propertySource) {
+        if (environment != null) {
+            environment.addPropertySource(propertySource);
+        }
+    }
+
     /**
      * Returns {@code true} if the specified property is defined, regardless of its value (it may not have a value).
      *
@@ -435,7 +446,7 @@ public final class PropertiesUtil {
      */
     private static class Environment {
 
-        private final Set<PropertySource> sources = new TreeSet<>(new PropertySource.Comparator());
+        private final Set<PropertySource> sources = new ConcurrentSkipListSet<>(new PropertySource.Comparator());
         /**
          * Maps a key to its value in the lowest priority source that contains it.
          */
@@ -464,6 +475,14 @@ public final class PropertiesUtil {
             reload();
         }
 
+        /**
+         * Allow a PropertySource to be added.
+         * @param propertySource The PropertySource to add.
+         */
+        public void addPropertySource(PropertySource propertySource) {
+            sources.add(propertySource);
+        }
+
         private synchronized void reload() {
             literal.clear();
             normalized.clear();
diff --git a/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/Log4j2SpringBootLoggingSystem.java b/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/Log4j2SpringBootLoggingSystem.java
index 0d9bf1b3a7..062fc02ef9 100644
--- a/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/Log4j2SpringBootLoggingSystem.java
+++ b/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/Log4j2SpringBootLoggingSystem.java
@@ -37,6 +37,7 @@ import org.springframework.boot.logging.LoggingSystem;
 import org.springframework.boot.logging.LoggingSystemFactory;
 import org.springframework.boot.logging.log4j2.Log4J2LoggingSystem;
 import org.springframework.core.annotation.Order;
+import org.springframework.core.env.Environment;
 import org.springframework.util.Assert;
 import org.springframework.util.ClassUtils;
 import org.springframework.util.ResourceUtils;
@@ -87,7 +88,9 @@ public class Log4j2SpringBootLoggingSystem extends Log4J2LoggingSystem {
      */
     @Override
     public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) {
-        getLoggerContext().putObjectIfAbsent(ENVIRONMENT_KEY, initializationContext.getEnvironment());
+        Environment environment = initializationContext.getEnvironment();
+        PropertiesUtil.getProperties().addPropertySource(new SpringPropertySource(environment));
+        getLoggerContext().putObjectIfAbsent(ENVIRONMENT_KEY, environment);
         super.initialize(initializationContext, configLocation, logFile);
     }
 
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 4d6712b4c7..23ad07060d 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
@@ -26,6 +26,12 @@ public class SpringPropertySource extends SpringEnvironmentHolder implements Pro
 
     private static final int DEFAULT_PRIORITY = -100;
 
+    private final Environment environment;
+
+    public SpringPropertySource(Environment environment) {
+        this.environment = environment;
+    }
+
     /**
      * System properties take precedence followed by properties in Log4j properties files. Spring properties
      * follow.
@@ -38,7 +44,6 @@ public class SpringPropertySource extends SpringEnvironmentHolder implements Pro
 
     @Override
     public String getProperty(String key) {
-        Environment environment = getEnvironment();
         if (environment != null) {
             return environment.getProperty(key);
         }
@@ -47,7 +52,6 @@ public class SpringPropertySource extends SpringEnvironmentHolder implements Pro
 
     @Override
     public boolean containsProperty(String key) {
-        Environment environment = getEnvironment();
         if (environment != null) {
             return environment.containsProperty(key);
         }
diff --git a/log4j-spring-boot/src/main/resources/META-INF/services/org.apache.logging.log4j.util.PropertySource b/log4j-spring-boot/src/main/resources/META-INF/services/org.apache.logging.log4j.util.PropertySource
deleted file mode 100644
index 890387156f..0000000000
--- a/log4j-spring-boot/src/main/resources/META-INF/services/org.apache.logging.log4j.util.PropertySource
+++ /dev/null
@@ -1,15 +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.
-org.apache.logging.log4j.spring.boot.SpringPropertySource
diff --git a/log4j-spring-boot/src/test/java/org/apache/logging/log4j/spring/boot/Log4j2CloudConfigLoggingSystemTest.java b/log4j-spring-boot/src/test/java/org/apache/logging/log4j/spring/boot/Log4j2SpringBootLoggingSystemTest.java
similarity index 98%
rename from log4j-spring-boot/src/test/java/org/apache/logging/log4j/spring/boot/Log4j2CloudConfigLoggingSystemTest.java
rename to log4j-spring-boot/src/test/java/org/apache/logging/log4j/spring/boot/Log4j2SpringBootLoggingSystemTest.java
index a4da7822ff..83ca4b329c 100644
--- a/log4j-spring-boot/src/test/java/org/apache/logging/log4j/spring/boot/Log4j2CloudConfigLoggingSystemTest.java
+++ b/log4j-spring-boot/src/test/java/org/apache/logging/log4j/spring/boot/Log4j2SpringBootLoggingSystemTest.java
@@ -29,7 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 import java.util.Arrays;
 import java.util.List;
 
-public class Log4j2CloudConfigLoggingSystemTest {
+public class Log4j2SpringBootLoggingSystemTest {
 
     @Test
     public void getStandardConfigLocations() {
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 962fd06381..a2a0066824 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -202,6 +202,9 @@
       </action>
     </release>
     <release version="2.19.0" date="2022-MM-DD" description="GA Release 2.19.0">
+      <action issue="LOG4J2-3588" dev="rgoers" type="update">
+        Allow PropertySources to be added.
+      </action>
       <action issue="LOG4J2-3578" dev="rgoers" type="fix">
         Generate new SSL certs for testing.
       </action>