This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/2.x by this push:
new abcb38f9cc fix: treat null parameters as empty in `CsvParameterLayout`
(#4245)
abcb38f9cc is described below
commit abcb38f9ccbe52542b660f939cef4066b6012c66
Author: dev_Hakaze <[email protected]>
AuthorDate: Thu Aug 27 19:03:27 2026 +0700
fix: treat null parameters as empty in `CsvParameterLayout` (#4245)
Co-authored-by: Volkan Yazıcı <[email protected]>
---
.../log4j/core/layout/CsvParameterLayoutTest.java | 16 ++++++++++++++++
.../logging/log4j/core/layout/CsvParameterLayout.java | 3 ++-
.../4243_fix_CsvParameterLayout_null_parameters.xml | 13 +++++++++++++
3 files changed, 31 insertions(+), 1 deletion(-)
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 50bff2beac..466e5ba4b1 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.commons.csv.CSVFormat;
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 class CsvParameterLayoutTest {
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 c680ab792d..c738c80137 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.PluginConfiguration;
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 class CsvParameterLayout extends AbstractCsvLayout {
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 0000000000..2849ce03f2
--- /dev/null
+++ b/src/changelog/.2.x.x/4243_fix_CsvParameterLayout_null_parameters.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns="https://logging.apache.org/xml/ns"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ https://logging.apache.org/xml/ns
+ https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+ type="fixed">
+ <issue id="4243"
link="https://github.com/apache/logging-log4j2/issues/4243"/>
+ <issue id="4245" link="https://github.com/apache/logging-log4j2/pull/4245"/>
+ <description format="asciidoc">
+ Fix `NullPointerException` in `CsvParameterLayout` when a log event has no
parameters (for example `SimpleMessage`).
+ </description>
+</entry>