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

logging-log4j2 git commit: [LOG4J2-1108] NullPointerException when passing null to java.util.logging.Logger.setLevel().

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 2b1b35613 -> 81e2fa4c3


[LOG4J2-1108] NullPointerException when passing null to
java.util.logging.Logger.setLevel().

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/81e2fa4c
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/81e2fa4c
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/81e2fa4c

Branch: refs/heads/master
Commit: 81e2fa4c347f92ecb05aa000e939adfe310f935a
Parents: 2b1b356
Author: ggregory <gg...@apache.org>
Authored: Fri Aug 28 11:42:56 2015 -0700
Committer: ggregory <gg...@apache.org>
Committed: Fri Aug 28 11:42:56 2015 -0700

----------------------------------------------------------------------
 .../org/apache/logging/log4j/core/Logger.java   |  9 +++++-
 .../log4j/jul/DefaultLevelConverter.java        |  3 ++
 .../logging/log4j/jul/LevelConverter.java       |  2 +-
 .../logging/log4j/jul/LevelTranslator.java      |  4 +--
 .../logging/log4j/jul/CoreLoggerTest.java       | 20 +++++++++++++
 .../log4j/jul/DefaultLevelConverterTest.java    | 31 ++++++++++++++++++++
 src/changes/changes.xml                         |  3 ++
 7 files changed, 68 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/81e2fa4c/log4j-core/src/main/java/org/apache/logging/log4j/core/Logger.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/Logger.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/Logger.java
index 81a86c9..1e02387 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/Logger.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/Logger.java
@@ -97,11 +97,18 @@ public class Logger extends AbstractLogger {
 
     /**
      * This method is not exposed through the public API and is provided primarily for unit testing.
-     * @param level The Level to use on this Logger.
+     * <p>
+     * If the new level is null, this logger inherits the level from its parent.
+     * </p>
+     * 
+     * @param level The Level to use on this Logger, may be null.
      */
     public synchronized void setLevel(final Level level) {
         if (level != null) {
             config = new PrivateConfig(config, level);
+        } else {
+            // Reusing parent PC, should we make a copy instead?
+            config = getParent().config;
         }
     }
 

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/81e2fa4c/log4j-jul/src/main/java/org/apache/logging/log4j/jul/DefaultLevelConverter.java
----------------------------------------------------------------------
diff --git a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/DefaultLevelConverter.java b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/DefaultLevelConverter.java
index 8648627..23ea0f9 100644
--- a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/DefaultLevelConverter.java
+++ b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/DefaultLevelConverter.java
@@ -116,6 +116,9 @@ public class DefaultLevelConverter implements LevelConverter {
 
     @Override
     public Level toLevel(final java.util.logging.Level javaLevel) {
+        if (javaLevel == null) {
+            return null;
+        }
         final Level level = julToLog4j.get(javaLevel);
         if (level != null) {
             return level;

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/81e2fa4c/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelConverter.java
----------------------------------------------------------------------
diff --git a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelConverter.java b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelConverter.java
index 22ddad6..95676f6 100644
--- a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelConverter.java
+++ b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelConverter.java
@@ -31,7 +31,7 @@ public interface LevelConverter {
     /**
      * Converts a JDK logging Level to a Log4j logging Level.
      *
-     * @param javaLevel JDK Level to convert.
+     * @param javaLevel JDK Level to convert, may be null per the JUL specification.
      * @return converted Level or {@code null} if the given level could not be converted.
      */
     Level toLevel(java.util.logging.Level javaLevel);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/81e2fa4c/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java
----------------------------------------------------------------------
diff --git a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java
index c64c380..328e1e5 100644
--- a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java
+++ b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java
@@ -65,8 +65,8 @@ public final class LevelTranslator {
     /**
      * Converts a JDK logging Level to a Log4j logging Level.
      *
-     * @param level JDK Level to convert.
-     * @return converted Level.
+     * @param level JDK Level to convert, may be null per the JUL specification.
+     * @return converted Level or null
      */
     public static Level toLevel(final java.util.logging.Level level) {
         return LEVEL_CONVERTER.toLevel(level);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/81e2fa4c/log4j-jul/src/test/java/org/apache/logging/log4j/jul/CoreLoggerTest.java
----------------------------------------------------------------------
diff --git a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/CoreLoggerTest.java b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/CoreLoggerTest.java
index 297afa5..ccb761f 100644
--- a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/CoreLoggerTest.java
+++ b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/CoreLoggerTest.java
@@ -18,6 +18,7 @@
 package org.apache.logging.log4j.jul;
 
 import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThat;
 
 import java.util.logging.Level;
@@ -73,4 +74,23 @@ public class CoreLoggerTest extends AbstractLoggerTest {
         assertThat(logger.getLevel(), equalTo(Level.FINE));
         assertThat(childLogger.getLevel(), equalTo(Level.FINE));
     }
+
+    @Test
+    public void testSetLevelToNull() throws Exception {
+        final Logger childLogger = Logger.getLogger(LOGGER_NAME + ".NullChild");
+        assertThat(childLogger.getLevel(), equalTo(Level.FINE));
+        assertThat(childLogger.isLoggable(Level.FINE), is(true));
+        childLogger.setLevel(Level.SEVERE);
+        assertThat(childLogger.getLevel(), equalTo(Level.SEVERE));
+        assertThat(childLogger.isLoggable(Level.FINE), is(false));
+        // null test
+        childLogger.setLevel(null);
+        assertThat(childLogger.getLevel(), equalTo(null));
+        assertThat(childLogger.isLoggable(Level.FINE), is(true));
+        // now go back
+        childLogger.setLevel(Level.SEVERE);
+        assertThat(childLogger.getLevel(), equalTo(Level.SEVERE));
+        assertThat(childLogger.isLoggable(Level.FINE), is(false));
+    }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/81e2fa4c/log4j-jul/src/test/java/org/apache/logging/log4j/jul/DefaultLevelConverterTest.java
----------------------------------------------------------------------
diff --git a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/DefaultLevelConverterTest.java b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/DefaultLevelConverterTest.java
new file mode 100644
index 0000000..801749e
--- /dev/null
+++ b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/DefaultLevelConverterTest.java
@@ -0,0 +1,31 @@
+/*
+ * 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.jul;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DefaultLevelConverterTest {
+
+    /**
+     * (LOG4J2-1108) NullPointerException when passing null to java.util.logging.Logger.setLevel().
+     */
+    @Test
+    public void testJulSetNull() {
+        Assert.assertEquals(null, new DefaultLevelConverter().toLevel(null));
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/81e2fa4c/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 509a993..f33e78d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -45,6 +45,9 @@
       <action issue="LOG4J2-1044" dev="rgoers" type="fix">
         Write pending events to Flume when the appender is stopped.
       </action>
+      <action issue="LOG4J2-1108" dev="ggregory" type="fix" due-to="Mikael Ståldal">
+        NullPointerException when passing null to java.util.logging.Logger.setLevel().
+      </action>
       <action dev="rpopma" type="remove">
         Removed experimental interface LevelLogger which got committed to master by mistake.
       </action>