Skip to content

Commit

Permalink
Support pattern-formatting in additional properties (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixbarny authored Sep 18, 2019
1 parent 43f3803 commit 33ae6d4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
import org.apache.logging.log4j.core.layout.ByteBufferDestination;
import org.apache.logging.log4j.core.layout.Encoder;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
import org.apache.logging.log4j.core.pattern.PatternFormatter;
import org.apache.logging.log4j.core.util.KeyValuePair;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.MultiformatMessage;
Expand Down Expand Up @@ -78,6 +80,7 @@ public void accept(final String key, final Object value, final StringBuilder str
};

private final KeyValuePair[] additionalFields;
private final PatternFormatter[][] fieldValuePatternFormatter;
private final Set<String> topLevelLabels;
private final boolean stackTraceAsArray;
private String serviceName;
Expand All @@ -93,6 +96,15 @@ private EcsLayout(Configuration config, String serviceName, boolean includeMarke
this.topLevelLabels.add("trace.id");
this.topLevelLabels.add("transaction.id");
this.additionalFields = additionalFields;
fieldValuePatternFormatter = new PatternFormatter[additionalFields.length][];
for (int i = 0; i < additionalFields.length; i++) {
KeyValuePair additionalField = additionalFields[i];
if (additionalField.getValue().contains("%")) {
fieldValuePatternFormatter[i] = PatternLayout.createPatternParser(config)
.parse(additionalField.getValue())
.toArray(new PatternFormatter[0]);
}
}
}

@PluginBuilderFactory
Expand Down Expand Up @@ -132,12 +144,21 @@ private StringBuilder toText(LogEvent event, StringBuilder builder, boolean gcFr
}

private void serializeLabels(LogEvent event, StringBuilder builder) {
if (!event.getContextData().isEmpty() || additionalFields.length > 0) {
if (additionalFields.length > 0) {
final int length = additionalFields.length;
if (!event.getContextData().isEmpty() || length > 0) {
if (length > 0) {
final StrSubstitutor strSubstitutor = getConfiguration().getStrSubstitutor();
for (KeyValuePair additionalField : additionalFields) {
for (int i = 0; i < length; i++) {
KeyValuePair additionalField = additionalFields[i];
PatternFormatter[] formatters = fieldValuePatternFormatter[i];
CharSequence value = null;
if (valueNeedsLookup(additionalField.getValue())) {
if (formatters != null) {
StringBuilder buffer = EcsJsonSerializer.getMessageStringBuilder();
formatPattern(event, formatters, buffer);
if (buffer.length() > 0) {
value = buffer;
}
} else if (valueNeedsLookup(additionalField.getValue())) {
StringBuilder lookupValue = EcsJsonSerializer.getMessageStringBuilder();
lookupValue.append(additionalField.getValue());
if (strSubstitutor.replaceIn(event, lookupValue)) {
Expand All @@ -160,6 +181,13 @@ private void serializeLabels(LogEvent event, StringBuilder builder) {
}
}

private static void formatPattern(LogEvent event, PatternFormatter[] formatters, StringBuilder buffer) {
final int len = formatters.length;
for (int i = 0; i < len; i++) {
formatters[i].format(event, buffer);
}
}

private void serializeTags(LogEvent event, StringBuilder builder) {
List<String> contextStack = event.getContextStack().asList();
Marker marker = event.getMarker();
Expand All @@ -169,7 +197,8 @@ private void serializeTags(LogEvent event, StringBuilder builder) {
}

if (!contextStack.isEmpty()) {
for (int i = 0; i < contextStack.size(); i++) {
final int len = contextStack.size();
for (int i = 0; i < len; i++) {
builder.append('\"');
JsonUtils.quoteAsString(contextStack.get(i), builder);
builder.append("\",");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ void globalLabels() throws Exception {
debug("test");
assertThat(getLastLogLine().get("cluster.uuid").textValue()).isEqualTo("9fe9134b-20b0-465e-acf9-8cc09ac9053b");
assertThat(getLastLogLine().get("node.id").textValue()).isEqualTo("foo");
assertThat(getLastLogLine().get("empty")).isNull();
assertThat(getLastLogLine().get("emptyPattern")).isNull();
assertThat(getLastLogLine().get("clazz").textValue()).startsWith(getClass().getPackageName());
assertThat(getLastLogLine().get("404")).isNull();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ void setUp() {
new KeyValuePair("cluster.uuid", "9fe9134b-20b0-465e-acf9-8cc09ac9053b"),
new KeyValuePair("node.id", "${node.id}"),
new KeyValuePair("empty", "${empty}"),
new KeyValuePair("clazz", "%C"),
new KeyValuePair("emptyPattern", "%notEmpty{%invalidPattern}"),
})
.build();

Expand Down
2 changes: 2 additions & 0 deletions log4j2-ecs-layout/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<KeyValuePair key="cluster.uuid" value="9fe9134b-20b0-465e-acf9-8cc09ac9053b"/>
<KeyValuePair key="node.id" value="${node.id}"/>
<KeyValuePair key="empty" value="${empty}"/>
<KeyValuePair key="clazz" value="%C"/>
<KeyValuePair key="emptyPattern" value="%notEmpty{%invalidPattern}"/>
</EcsLayout>
</List>
</Appenders>
Expand Down

0 comments on commit 33ae6d4

Please sign in to comment.