Skip to content

Commit 5fcfb80

Browse files
Piotr P. Karwaszppkarwasz
Piotr P. Karwasz
authored andcommitted
[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.
1 parent 64226fe commit 5fcfb80

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLogger.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*
3333
* @author <a href="http://www.vorburger.ch">Michael Vorburger.ch</a> for Google
3434
*/
35-
public final class JULLogger extends AbstractLogger {
35+
final class JULLogger extends AbstractLogger {
3636
private static final long serialVersionUID = 1L;
3737

3838
private final Logger logger;
@@ -162,7 +162,8 @@ private java.util.logging.Level getEffectiveJULLevel() {
162162
}
163163
// This is a safety fallback that is typically never reached, because usually the root Logger.getLogger("") has
164164
// a Level.
165-
return Logger.getGlobal().getLevel();
165+
// Since JDK 8 the LogManager$RootLogger does not have a default level, just a default effective level of INFO.
166+
return java.util.logging.Level.INFO;
166167
}
167168

168169
private boolean isEnabledFor(final Level level, final Marker marker) {

log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @author <a href="http://www.vorburger.ch">Michael Vorburger.ch</a> for Google
2222
*/
2323
@Export
24-
@Version("2.20.1")
24+
@Version("3.0.0")
2525
package org.apache.logging.log4j.tojul;
2626

2727
import org.osgi.annotation.bundle.Export;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.tojul;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
import org.apache.logging.log4j.Level;
22+
import org.junit.jupiter.api.Test;
23+
24+
public class JULLoggerTest {
25+
26+
@Test
27+
public void testNotNullEffectiveLevel() {
28+
// Emulates the root logger found in Tomcat, with a null level
29+
// See: https://bz.apache.org/bugzilla/show_bug.cgi?id=66184
30+
final java.util.logging.Logger julLogger = new java.util.logging.Logger("", null) {};
31+
final JULLogger logger = new JULLogger("", julLogger);
32+
assertEquals(Level.INFO, logger.getLevel());
33+
}
34+
}

0 commit comments

Comments
 (0)