Skip to content

Commit 99e23b1

Browse files
committed
Document and fix topLevelLabels
1 parent 76f0ce6 commit 99e23b1

File tree

6 files changed

+22
-10
lines changed

6 files changed

+22
-10
lines changed

ecs-logging-core/src/test/java/co/elastic/logging/AbstractEcsLoggingTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ void testThreadContextStack() throws Exception {
7676
@Test
7777
void testTopLevelLabels() throws Exception {
7878
putMdc("transaction.id", "0af7651916cd43dd8448eb211c80319c");
79+
putMdc("span.id", "foo");
7980
debug("test");
8081
assertThat(getLastLogLine().get("labels.transaction.id")).isNull();
8182
assertThat(getLastLogLine().get("transaction.id").textValue()).isEqualTo("0af7651916cd43dd8448eb211c80319c");
83+
assertThat(getLastLogLine().get("span.id").textValue()).isEqualTo("foo");
8284
}
8385

8486
@Test

log4j2-ecs-layout/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Instead of the usual `<PatternLayout/>`, use `<EcsLayout serviceName="my-app"/>`
4545
|Parameter name |Type |Default|Description|
4646
|-----------------|-------|-------|-----------|
4747
|serviceName |String | |Sets the `service.name` field so you can filter your logs by a particular service |
48+
|topLevelLabels |String |`trace.id, transaction.id, span.id, error.id, service.name`|Usually, MDC keys are nested under `labels`[https://www.elastic.co/guide/en/ecs/current/ecs-base.html]. You can specify a comma-separated list of properties which should be on the top level. |
4849
|includeMarkers |boolean|`false`|Log [Markers](https://logging.apache.org/log4j/2.0/manual/markers.html) as `tags` |
4950
|stackTraceAsArray|boolean|`false`|Serializes the `error.stack_trace` as a JSON array where each element is in a new line to improve readability. Note that this requires a slightly more complex [Filebeat configuration](../README.md#when-stacktraceasarray-is-enabled).|
5051
|includeOrigin |boolean|`false`|If `true`, adds the `log.origin.file`, `log.origin.function` and `log.origin.line` fields. Note that you also have to set `includeLocation="true"` on your loggers and appenders if you are using the async ones. |

log4j2-ecs-layout/src/main/java/co/elastic/logging/log4j2/EcsLayout.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@
5050
import org.apache.logging.log4j.util.TriConsumer;
5151

5252
import java.nio.charset.Charset;
53-
import java.util.Arrays;
53+
import java.util.ArrayList;
5454
import java.util.Collection;
55-
import java.util.Collections;
5655
import java.util.HashSet;
5756
import java.util.List;
5857
import java.util.Set;
@@ -93,10 +92,9 @@ private EcsLayout(Configuration config, String serviceName, boolean includeMarke
9392
this.serviceName = serviceName;
9493
this.includeMarkers = includeMarkers;
9594
this.topLevelLabels = new HashSet<String>(topLevelLabels);
95+
this.topLevelLabels.addAll(EcsJsonSerializer.DEFAULT_TOP_LEVEL_LABELS);
9696
this.includeOrigin = includeOrigin;
9797
this.stackTraceAsArray = stackTraceAsArray;
98-
this.topLevelLabels.add("trace.id");
99-
this.topLevelLabels.add("transaction.id");
10098
this.additionalFields = additionalFields;
10199
fieldValuePatternFormatter = new PatternFormatter[additionalFields.length][];
102100
for (int i = 0; i < additionalFields.length; i++) {
@@ -313,8 +311,8 @@ public static class Builder extends AbstractStringLayout.Builder<EcsLayout.Build
313311
private boolean stackTraceAsArray = false;
314312
@PluginElement("AdditionalField")
315313
private KeyValuePair[] additionalFields;
316-
@PluginElement("TopLevelLabels")
317-
private String[] topLevelLabels;
314+
@PluginBuilderAttribute("topLevelLabels")
315+
private String topLevelLabels;
318316
@PluginBuilderAttribute("includeOrigin")
319317
private boolean includeOrigin;
320318

@@ -339,11 +337,11 @@ public boolean isIncludeOrigin() {
339337
return includeOrigin;
340338
}
341339

342-
public String[] getTopLevelLabels() {
340+
public String getTopLevelLabels() {
343341
return topLevelLabels;
344342
}
345343

346-
public EcsLayout.Builder setTopLevelLabels(final String[] topLevelLabels) {
344+
public EcsLayout.Builder setTopLevelLabels(final String topLevelLabels) {
347345
this.topLevelLabels = topLevelLabels;
348346
return asBuilder();
349347
}
@@ -380,7 +378,13 @@ public EcsLayout.Builder setStackTraceAsArray(boolean stackTraceAsArray) {
380378

381379
@Override
382380
public EcsLayout build() {
383-
return new EcsLayout(getConfiguration(), serviceName, includeMarkers, additionalFields, topLevelLabels == null ? Collections.<String>emptyList() : Arrays.<String>asList(topLevelLabels), includeOrigin, stackTraceAsArray);
381+
List<String> topLevelLabelsList = new ArrayList<String>();
382+
if (topLevelLabels != null) {
383+
for (String label : topLevelLabels.split(",")) {
384+
topLevelLabelsList.add(label.trim());
385+
}
386+
}
387+
return new EcsLayout(getConfiguration(), serviceName, includeMarkers, additionalFields, topLevelLabelsList, includeOrigin, stackTraceAsArray);
384388
}
385389

386390
public boolean isStackTraceAsArray() {

log4j2-ecs-layout/src/test/java/co/elastic/logging/log4j2/AbstractLog4j2EcsLayoutTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,17 @@ void tearDown() throws Exception {
5050
@Test
5151
void globalLabels() throws Exception {
5252
putMdc("trace.id", "foo");
53+
putMdc("top_level", "foo");
54+
putMdc("nested_under_labels", "foo");
5355
debug("test");
5456
assertThat(getLastLogLine().get("cluster.uuid").textValue()).isEqualTo("9fe9134b-20b0-465e-acf9-8cc09ac9053b");
5557
assertThat(getLastLogLine().get("node.id").textValue()).isEqualTo("foo");
5658
assertThat(getLastLogLine().get("empty")).isNull();
5759
assertThat(getLastLogLine().get("emptyPattern")).isNull();
5860
assertThat(getLastLogLine().get("clazz").textValue()).startsWith(getClass().getPackageName());
5961
assertThat(getLastLogLine().get("404")).isNull();
62+
assertThat(getLastLogLine().get("top_level").textValue()).isEqualTo("foo");
63+
assertThat(getLastLogLine().get("labels.nested_under_labels").textValue()).isEqualTo("foo");
6064
}
6165

6266
@Test

log4j2-ecs-layout/src/test/java/co/elastic/logging/log4j2/Log4j2EcsLayoutTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ void setUp() {
7070
.setIncludeMarkers(true)
7171
.setIncludeOrigin(true)
7272
.setStackTraceAsArray(true)
73+
.setTopLevelLabels("top_level")
7374
.setAdditionalFields(new KeyValuePair[]{
7475
new KeyValuePair("cluster.uuid", "9fe9134b-20b0-465e-acf9-8cc09ac9053b"),
7576
new KeyValuePair("node.id", "${node.id}"),

log4j2-ecs-layout/src/test/resources/log4j2-test.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</Properties>
66
<Appenders>
77
<List name="TestAppender">
8-
<EcsLayout serviceName="test" includeMarkers="true" includeOrigin="true" stackTraceAsArray="true">
8+
<EcsLayout serviceName="test" includeMarkers="true" includeOrigin="true" stackTraceAsArray="true" topLevelLabels="top_level">
99
<KeyValuePair key="cluster.uuid" value="9fe9134b-20b0-465e-acf9-8cc09ac9053b"/>
1010
<KeyValuePair key="node.id" value="${node.id}"/>
1111
<KeyValuePair key="empty" value="${empty}"/>

0 commit comments

Comments
 (0)