You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2022/07/28 20:22:02 UTC

[logging-log4j2] branch release-2.x updated: [LOG4J2-3564] Fix NPE when root logger level is null

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

pkarwasz 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 3df5ce9b1a [LOG4J2-3564] Fix NPE when root logger level is null
3df5ce9b1a is described below

commit 3df5ce9b1ab17c4c7f0cdd1caef18db959fef937
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Thu Jul 28 22:21:53 2022 +0200

    [LOG4J2-3564] Fix NPE when root logger level is null
    
    `LogManager.getGlobal().getLevel()` is not a good fallback for the case
    when all JUL logger levels are null, since its level is usually null.
---
 .../org/apache/logging/log4j/tojul/JULLogger.java  |  3 +-
 .../apache/logging/log4j/tojul/JULLoggerTest.java  | 35 ++++++++++++++++++++++
 src/changes/changes.xml                            |  3 ++
 3 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLogger.java b/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLogger.java
index 5f27ae3b71..1a67ea35a5 100644
--- a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLogger.java
+++ b/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLogger.java
@@ -153,7 +153,8 @@ final class JULLogger extends AbstractLogger {
             return current.getLevel();
         }
         // This is a safety fallback that is typically never reached, because usually the root Logger.getLogger("") has a Level.
-        return Logger.getGlobal().getLevel();
+        // Since JDK 8 the LogManager$RootLogger does not have a default level, just a default effective level of INFO.
+        return java.util.logging.Level.INFO;
     }
 
     private boolean isEnabledFor(final Level level, final Marker marker) {
diff --git a/log4j-to-jul/src/test/java/org/apache/logging/log4j/tojul/JULLoggerTest.java b/log4j-to-jul/src/test/java/org/apache/logging/log4j/tojul/JULLoggerTest.java
new file mode 100644
index 0000000000..1bd27422a4
--- /dev/null
+++ b/log4j-to-jul/src/test/java/org/apache/logging/log4j/tojul/JULLoggerTest.java
@@ -0,0 +1,35 @@
+/*
+ * 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.tojul;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.logging.log4j.Level;
+import org.junit.jupiter.api.Test;
+
+public class JULLoggerTest {
+
+    @Test
+    public void testNotNullEffectiveLevel() {
+        // Emulates the root logger found in Tomcat, with a null level
+        // See: https://bz.apache.org/bugzilla/show_bug.cgi?id=66184
+        final java.util.logging.Logger julLogger = new java.util.logging.Logger("", null) {
+        };
+        final JULLogger logger = new JULLogger("", julLogger);
+        assertEquals(Level.INFO, logger.getLevel());
+    }
+}
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 143ce9e597..5f0b59cd8e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -39,6 +39,9 @@
       <action issue="LOG4J2-3561" dev="pkarwasz" type="fix" due-to="Robert Papp">
         Allows a space separated list of style specifiers in the %style pattern for consistency with %highlight.
       </action>
+      <action issue="LOG4J2-3564" dev="pkarwasz" type="fix">
+        Fix NPE in `log4j-to-jul` in the case the root logger level is null.
+      </action>
     </release>
     <release version="2.18.0" date="2022-06-28" description="GA Release 2.18.0">
       <action issue="LOG4J2-3339" dev="rgoers" type="fix">