Skip to content
Open
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 @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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"/>
<description format="asciidoc">
Fix `NullPointerException` in `CsvParameterLayout` when a log event has no parameters (for example `SimpleMessage`).
</description>
</entry>