Skip to content

Commit 82451b7

Browse files
spinscalefelixbarny
authored andcommitted
Fix possible NPE in log4j2 builder (#33)
the builder no longer throws a NPE, when there are no additional fields configured.
1 parent ec715c6 commit 82451b7

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
target
2+
.idea
3+
**.iml

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,11 @@ public static class Builder extends AbstractStringLayout.Builder<EcsLayout.Build
310310
@PluginBuilderAttribute("stackTraceAsArray")
311311
private boolean stackTraceAsArray = false;
312312
@PluginElement("AdditionalField")
313-
private KeyValuePair[] additionalFields;
313+
private KeyValuePair[] additionalFields = new KeyValuePair[] {};
314314
@PluginBuilderAttribute("topLevelLabels")
315315
private String topLevelLabels;
316316
@PluginBuilderAttribute("includeOrigin")
317-
private boolean includeOrigin;
317+
private boolean includeOrigin = false;
318318

319319
Builder() {
320320
super();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*-
2+
* #%L
3+
* Java ECS logging
4+
* %%
5+
* Copyright (C) 2019 Elastic and contributors
6+
* %%
7+
* Licensed to Elasticsearch B.V. under one or more contributor
8+
* license agreements. See the NOTICE file distributed with
9+
* this work for additional information regarding copyright
10+
* ownership. Elasticsearch B.V. licenses this file to you under
11+
* the Apache License, Version 2.0 (the "License"); you may
12+
* not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing,
18+
* software distributed under the License is distributed on an
19+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
* KIND, either express or implied. See the License for the
21+
* specific language governing permissions and limitations
22+
* under the License.
23+
* #L%
24+
*/
25+
package co.elastic.logging.log4j2;
26+
27+
import org.junit.jupiter.api.Test;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
class Log4j2EcsBuilderTest {
32+
33+
@Test
34+
void testAdditionalFieldsCanBeEmptyInBuilder() {
35+
EcsLayout ecsLayout = EcsLayout.newBuilder().setServiceName("test").build();
36+
assertThat(ecsLayout).isNotNull();
37+
}
38+
}

0 commit comments

Comments
 (0)