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 2020/12/11 00:10:48 UTC

[logging-log4j2] branch release-2.x updated: Log4j would fail to initialize in Java 8 with log4j-spring-boot

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 52cfeff  Log4j would fail to initialize in Java 8 with log4j-spring-boot
52cfeff is described below

commit 52cfeff12c11ddc256973559c7b0b53317f6907c
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Thu Dec 10 15:22:37 2020 -0700

    Log4j would fail to initialize in Java 8 with log4j-spring-boot
---
 .../java/org/apache/logging/log4j/LogManager.java  |  2 ++
 .../logging/log4j/internal/LogManagerStatus.java   | 33 ++++++++++++++++++++++
 .../log4j/spring/boot/SpringEnvironmentHolder.java |  4 ++-
 src/changes/changes.xml                            |  3 ++
 4 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java b/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java
index d7bc9fd..46936c0 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java
@@ -21,6 +21,7 @@ import java.util.Map;
 import java.util.SortedMap;
 import java.util.TreeMap;
 
+import org.apache.logging.log4j.internal.LogManagerStatus;
 import org.apache.logging.log4j.message.MessageFactory;
 import org.apache.logging.log4j.message.StringFormatterMessageFactory;
 import org.apache.logging.log4j.simple.SimpleLoggerContextFactory;
@@ -121,6 +122,7 @@ public class LogManager {
                         + "Please add log4j-core to the classpath. Using SimpleLogger to log to the console...");
                 factory = new SimpleLoggerContextFactory();
             }
+            LogManagerStatus.setInitialized(true);
         }
     }
 
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/internal/LogManagerStatus.java b/log4j-api/src/main/java/org/apache/logging/log4j/internal/LogManagerStatus.java
new file mode 100644
index 0000000..b793927
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/internal/LogManagerStatus.java
@@ -0,0 +1,33 @@
+/*
+ * 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.internal;
+
+/**
+ * Keeps track of LogManager initialization status;
+ */
+public class LogManagerStatus {
+
+    private static boolean initialized = false;
+
+    public static void setInitialized(boolean managerStatus) {
+        initialized = managerStatus;
+    }
+
+    public static boolean isInitialized() {
+        return initialized;
+    }
+}
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
index e7fe436..5c4403d 100644
--- 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
@@ -20,6 +20,7 @@ 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;
 
 /**
@@ -32,7 +33,8 @@ public class SpringEnvironmentHolder {
 
 
     protected Environment getEnvironment() {
-        if (environment == null && LogManager.getFactory() != null && LogManager.getFactory().hasContext(SpringEnvironmentHolder.class.getName(), null, false)) {
+        if (environment == null && LogManagerStatus.isInitialized() && LogManager.getFactory() != null &&
+                LogManager.getFactory().hasContext(SpringEnvironmentHolder.class.getName(), null, false)) {
             lock.lock();
             try {
                 if (environment == null) {
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7e276ac..6db8070 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,6 +30,9 @@
          - "remove" - Removed
     -->
     <release version="2.14.1" date="2020-MM-DD" description="GA Release 2.14.1">
+      <action issue="LOG4J2-2974" dev="rgoers" type="fix">
+        Log4j would fail to initialize in Java 8 with log4j-spring-boot.
+      </action>
       <action issue="LOG4J2-2962" dev="vy" type="add">
         Enrich "map" resolver by unifying its backend with "mdc" resolver.
       </action>