Skip to content

Prevent NPE with rootLogger shorthand when no appenders are specified #3650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package org.apache.logging.log4j.core.config;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand All @@ -32,6 +34,7 @@
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.config.properties.PropertiesConfiguration;
import org.apache.logging.log4j.core.impl.Log4jLogEvent.Builder;
import org.apache.logging.log4j.message.SimpleMessage;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -137,4 +140,18 @@ void testSingleFilterInvocation() {
verify(appender, times(1)).append(any());
verify(filter, times(1)).filter(any());
}

@Test
void testLevelAndRefsWithoutAppenderRef() {
final Configuration configuration = mock(PropertiesConfiguration.class);
final LoggerConfig.Builder builder = LoggerConfig.newBuilder()
.withLoggerName(FQCN)
.withConfig(configuration)
.withLevelAndRefs(Level.INFO.name());

final LoggerConfig loggerConfig = builder.build();

assertNotNull(loggerConfig.getAppenderRefs());
assertTrue(loggerConfig.getAppenderRefs().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1009,13 +1009,13 @@ protected static LevelAndRefs getLevelAndRefs(
}
final String[] parts = Strings.splitList(levelAndRefs);
result.level = Level.getLevel(parts[0]);
final List<AppenderRef> refList = new ArrayList<>();
if (parts.length > 1) {
final List<AppenderRef> refList = new ArrayList<>();
Arrays.stream(parts)
.skip(1)
.forEach((ref) -> refList.add(AppenderRef.createAppenderRef(ref, null, null)));
result.refs = refList;
}
result.refs = refList;
} else {
LOGGER.warn("levelAndRefs are only allowed in a properties configuration. The value is ignored.");
result.level = level;
Expand Down
10 changes: 10 additions & 0 deletions src/changelog/.2.x.x/3206_fix_rootLogger_shorthand_npe.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
<issue id="3206" link="https://github.com/apache/logging-log4j2/issues/3206"/>
<description format="asciidoc">
Fix NullPointerException when using `rootLogger = LEVEL` shorthand in properties without appender.
</description>
</entry>