Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -313,6 +313,36 @@ void testCollection() {
}
}

@ParameterizedTest
@ValueSource(strings = {"Transfer", "Transfer@32473"})
void testCollectionMergesElementsWithSameId(final String id) {
final Rfc5424Layout layout = Rfc5424Layout.newBuilder().build();
final StructuredDataMessage msg = new StructuredDataMessage(id, "Transfer Complete", "Audit");
msg.put("Amount", "200.00");
final StructuredDataMessage msg2 = new StructuredDataMessage(id, null, "Audit");
msg2.put("ToAccount", "123456");
final LogEvent event = Log4jLogEvent.newBuilder()
.setMessage(new StructuredDataCollectionMessage(Arrays.asList(msg, msg2)))
.build();

assertThat(layout.toSerializable(event)).contains("[Transfer@32473 Amount=\"200.00\" ToAccount=\"123456\"]");
}

@Test
void testMdcElementMergesStructuredDataWithSameId() {
final Rfc5424Layout layout = Rfc5424Layout.newBuilder().build();
final StringMap contextData = ContextDataFactory.createContextData();
contextData.putValue("loginId", "JohnDoe");
final StructuredDataMessage msg = new StructuredDataMessage("mdc", "Transfer Complete", "Audit");
msg.put("Amount", "200.00");
final LogEvent event = Log4jLogEvent.newBuilder()
.setMessage(msg)
.setContextData(contextData)
.build();

assertThat(layout.toSerializable(event)).contains("[mdc@32473 Amount=\"200.00\" loginId=\"JohnDoe\"]");
}

/**
* Test case for escaping newlines and other SD PARAM-NAME special characters.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ private void addStructuredData(
final String sdId = getId(id);

if (sdElements.containsKey(sdId)) {
final StructuredDataElement union = sdElements.get(id.toString());
final StructuredDataElement union = sdElements.get(sdId);
union.union(map);
sdElements.put(sdId, union);
} else {
Expand Down Expand Up @@ -1214,7 +1214,7 @@ public StructuredDataElement format(final LogEvent event) {

private class StructuredDataElement {

private final Map<String, String> fields;
private Map<String, String> fields;
private final boolean discardIfEmpty;
private final String prefix;

Expand All @@ -1240,7 +1240,10 @@ boolean discard() {
}

void union(final Map<String, String> addFields) {
this.fields.putAll(addFields);
// The current fields might be unmodifiable, e.g. those of a `StructuredDataMessage`
final Map<String, String> union = new HashMap<>(fields);
union.putAll(addFields);
this.fields = union;
}

Map<String, String> getFields() {
Expand Down
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="4420" link="https://github.com/apache/logging-log4j2/pull/4420"/>
<description format="asciidoc">
Fix `Rfc5424Layout` failing on two SD elements with one SD-ID.
</description>
</entry>
Loading