From 49a62de92aed3214669d000d5798e63bcc696bf0 Mon Sep 17 00:00:00 2001
From: arimu1 <19286898+arimu1@users.noreply.github.com>
Date: Thu, 6 Aug 2026 08:49:32 +0700
Subject: [PATCH 1/2] fix: treat null parameters as empty in CsvParameterLayout
SimpleMessage and other parameter-less Message implementations return
null from getParameters(). Commons CSV printRecord NPEs on a null values
array, so plain logger.info("text") events failed the layout. Use
Constants.EMPTY_OBJECT_ARRAY so an empty CSV record is emitted instead.
Fixes #4243
Signed-off-by: arimu1 <19286898+arimu1@users.noreply.github.com>
---
.../core/layout/CsvParameterLayoutTest.java | 16 ++++++++++++++++
.../log4j/core/layout/CsvParameterLayout.java | 3 ++-
...43_fix_CsvParameterLayout_null_parameters.xml | 12 ++++++++++++
3 files changed, 30 insertions(+), 1 deletion(-)
create mode 100644 src/changelog/.2.x.x/4243_fix_CsvParameterLayout_null_parameters.xml
diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/layout/CsvParameterLayoutTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/layout/CsvParameterLayoutTest.java
index 50bff2beac7..466e5ba4b1a 100644
--- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/layout/CsvParameterLayoutTest.java
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/layout/CsvParameterLayoutTest.java
@@ -29,11 +29,14 @@
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.categories.Layouts;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.apache.logging.log4j.message.ObjectArrayMessage;
+import org.apache.logging.log4j.message.SimpleMessage;
import org.apache.logging.log4j.test.junit.ThreadContextRule;
import org.junit.Assert;
import org.junit.Rule;
@@ -170,6 +173,19 @@ public void testLayoutTab() throws Exception {
testLayoutNormalApi(root, CsvParameterLayout.createLayout(CSVFormat.TDF), true);
}
+ @Test
+ public void testNullParametersProduceEmptyRecord() {
+ // SimpleMessage#getParameters() returns null; must not NPE (GH-4243)
+ final AbstractCsvLayout layout = CsvParameterLayout.createDefaultLayout();
+ final LogEvent event = Log4jLogEvent.newBuilder()
+ .setLoggerName("test")
+ .setLevel(Level.INFO)
+ .setMessage(new SimpleMessage("plain text without parameters"))
+ .build();
+ final String result = layout.toSerializable(event);
+ Assert.assertEquals(layout.getFormat().getRecordSeparator(), result);
+ }
+
@Test
public void testLogJsonArgument() throws InterruptedException {
final ListAppender appender = init.getAppender("List");
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/CsvParameterLayout.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/CsvParameterLayout.java
index c680ab792d0..c738c80137e 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/CsvParameterLayout.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/CsvParameterLayout.java
@@ -30,6 +30,7 @@
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.Constants;
/**
* A Comma-Separated Value (CSV) layout to log event parameters.
@@ -95,7 +96,7 @@ public String toSerializable(final LogEvent event) {
final Object[] parameters = message.getParameters();
final StringBuilder buffer = getStringBuilder();
try {
- getFormat().printRecord(buffer, parameters);
+ getFormat().printRecord(buffer, parameters == null ? Constants.EMPTY_OBJECT_ARRAY : parameters);
return buffer.toString();
} catch (final IOException e) {
StatusLogger.getLogger().error(message, e);
diff --git a/src/changelog/.2.x.x/4243_fix_CsvParameterLayout_null_parameters.xml b/src/changelog/.2.x.x/4243_fix_CsvParameterLayout_null_parameters.xml
new file mode 100644
index 00000000000..57004d57d0c
--- /dev/null
+++ b/src/changelog/.2.x.x/4243_fix_CsvParameterLayout_null_parameters.xml
@@ -0,0 +1,12 @@
+
+
+
+
+ Fix `NullPointerException` in `CsvParameterLayout` when a log event has no parameters (for example `SimpleMessage`).
+
+
From 411009632404fcd0ee9dffd0fb008cd61eb1d031 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?=
Date: Thu, 27 Aug 2026 13:00:42 +0200
Subject: [PATCH 2/2] Add PR link to the changelog
---
.../.2.x.x/4243_fix_CsvParameterLayout_null_parameters.xml | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/changelog/.2.x.x/4243_fix_CsvParameterLayout_null_parameters.xml b/src/changelog/.2.x.x/4243_fix_CsvParameterLayout_null_parameters.xml
index 57004d57d0c..2849ce03f28 100644
--- a/src/changelog/.2.x.x/4243_fix_CsvParameterLayout_null_parameters.xml
+++ b/src/changelog/.2.x.x/4243_fix_CsvParameterLayout_null_parameters.xml
@@ -6,6 +6,7 @@
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
+
Fix `NullPointerException` in `CsvParameterLayout` when a log event has no parameters (for example `SimpleMessage`).