Skip to content

Commit 8a5bb2f

Browse files
authored
Simplify + Allow custom MDC lookup (#193)
1 parent 7e723e5 commit 8a5bb2f

File tree

5 files changed

+55
-157
lines changed

5 files changed

+55
-157
lines changed

jul-ecs-formatter/src/main/java/co/elastic/logging/jul/EcsFormatter.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import java.util.Collections;
3131
import java.util.List;
32+
import java.util.Map;
3233
import java.util.logging.Formatter;
3334
import java.util.logging.LogManager;
3435
import java.util.logging.LogRecord;
@@ -69,7 +70,7 @@ public String format(final LogRecord record) {
6970
EcsJsonSerializer.serializeFormattedMessage(builder, super.formatMessage(record));
7071
EcsJsonSerializer.serializeEcsVersion(builder);
7172
EcsJsonSerializer.serializeAdditionalFields(builder, additionalFields);
72-
EcsJsonSerializer.serializeMDC(builder, JulMdc.getEntries());
73+
EcsJsonSerializer.serializeMDC(builder, getMdcEntries());
7374
EcsJsonSerializer.serializeServiceName(builder, serviceName);
7475
EcsJsonSerializer.serializeServiceVersion(builder, serviceVersion);
7576
EcsJsonSerializer.serializeServiceEnvironment(builder, serviceEnvironment);
@@ -92,6 +93,16 @@ public String format(final LogRecord record) {
9293
return builder.toString();
9394
}
9495

96+
/**
97+
* Used by APM agent to provide MDC for JUL through instrumentation and to allow testing without an actual MDC
98+
* implementation.
99+
*
100+
* @return MDC entries
101+
*/
102+
protected Map<String, String> getMdcEntries() {
103+
return Collections.emptyMap();
104+
}
105+
95106
public void setIncludeOrigin(final boolean includeOrigin) {
96107
this.includeOrigin = includeOrigin;
97108
}

jul-ecs-formatter/src/main/java/co/elastic/logging/jul/JulMdc.java

Lines changed: 0 additions & 71 deletions
This file was deleted.

jul-ecs-formatter/src/test/java/co/elastic/logging/jul/EcsFormatterTest.java

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* the Apache License, Version 2.0 (the "License"); you may
1212
* not use this file except in compliance with the License.
1313
* You may obtain a copy of the License at
14-
*
14+
*
1515
* http://www.apache.org/licenses/LICENSE-2.0
16-
*
16+
*
1717
* Unless required by applicable law or agreed to in writing,
1818
* software distributed under the License is distributed on an
1919
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -24,12 +24,15 @@
2424
*/
2525
package co.elastic.logging.jul;
2626

27+
import com.fasterxml.jackson.core.JsonProcessingException;
2728
import com.fasterxml.jackson.databind.JsonNode;
2829
import com.fasterxml.jackson.databind.ObjectMapper;
2930
import org.junit.jupiter.api.BeforeEach;
3031
import org.junit.jupiter.api.Test;
3132

3233
import java.time.Instant;
34+
import java.util.HashMap;
35+
import java.util.Map;
3336
import java.util.logging.Level;
3437
import java.util.logging.LogRecord;
3538

@@ -40,7 +43,7 @@ public class EcsFormatterTest {
4043
private final EcsFormatter formatter = new EcsFormatter();
4144

4245
private final LogRecord record = new LogRecord(Level.INFO, "Example Message");
43-
private final ObjectMapper objectMapper = new ObjectMapper();
46+
private static final ObjectMapper objectMapper = new ObjectMapper();
4447

4548
@BeforeEach
4649
void setUp() {
@@ -57,21 +60,21 @@ public void testFormatWithIncludeOriginFlag() throws Exception {
5760

5861
final String result = formatter.format(record);
5962

60-
assertThat(objectMapper.readTree(result).at("/log/origin/file/name").textValue()).isEqualTo("ExampleClass.java");
61-
assertThat(objectMapper.readTree(result).at("/log/origin/function").textValue()).isEqualTo("exampleMethod");
63+
assertThat(parseJson(result).at("/log/origin/file/name").textValue()).isEqualTo("ExampleClass.java");
64+
assertThat(parseJson(result).at("/log/origin/function").textValue()).isEqualTo("exampleMethod");
6265
}
6366

6467
@Test
6568
public void testFormatWithoutIncludeOriginFlag() throws Exception {
66-
final JsonNode result = objectMapper.readTree(formatter.format(record));
69+
final JsonNode result = parseJson(formatter.format(record));
6770
assertThat(result.get("log.origin")).isNull();
6871
}
6972

7073
@Test
7174
public void testFormatWithoutLoggerName() throws Exception {
7275
record.setLoggerName(null);
7376

74-
final JsonNode result = objectMapper.readTree(formatter.format(record));
77+
final JsonNode result = parseJson(formatter.format(record));
7578

7679
assertThat(result.get("log.logger")).isNull();
7780
}
@@ -80,7 +83,7 @@ public void testFormatWithoutLoggerName() throws Exception {
8083
public void testFormatWithEmptyLoggerName() throws Exception {
8184
record.setLoggerName("");
8285

83-
final JsonNode result = objectMapper.readTree(formatter.format(record));
86+
final JsonNode result = parseJson(formatter.format(record));
8487

8588
assertThat(result.get("log.logger").textValue()).isEmpty();
8689
}
@@ -90,7 +93,7 @@ public void testFormatWithInnerClassName() throws Exception {
9093
formatter.setIncludeOrigin(true);
9194
record.setSourceClassName("test.ExampleClass$InnerClass");
9295

93-
JsonNode result = objectMapper.readTree(formatter.format(record));
96+
JsonNode result = parseJson(formatter.format(record));
9497
assertThat(result.at("/log/origin/file/name").textValue()).isEqualTo("ExampleClass.java");
9598
assertThat(result.at("/log/origin/function").textValue()).isEqualTo("exampleMethod");
9699
}
@@ -100,9 +103,39 @@ public void testFormatWithInvalidClassName() throws Exception {
100103
formatter.setIncludeOrigin(true);
101104
record.setSourceClassName("$test.ExampleClass");
102105

103-
JsonNode result = objectMapper.readTree(formatter.format(record));
106+
JsonNode result = parseJson(formatter.format(record));
104107
assertThat(result.at("/log/origin/file/name").textValue()).isEqualTo("<Unknown>");
105108
assertThat(result.at("/log/origin/function").textValue()).isEqualTo("exampleMethod");
106109
}
107110

111+
@Test
112+
void testMdcSerialization_singleEntry() {
113+
Map<String,String> mdc = new HashMap<>();
114+
TestMdcEcsFormatter mdcFormatter = new TestMdcEcsFormatter(mdc);
115+
mdc.put("mdc.key", "value");
116+
JsonNode result = parseJson(mdcFormatter.format(record));
117+
assertThat(result.get("mdc.key").textValue()).isEqualTo("value");
118+
}
119+
120+
private static JsonNode parseJson(String formatter) {
121+
try {
122+
return objectMapper.readTree(formatter);
123+
} catch (JsonProcessingException e) {
124+
throw new RuntimeException(e);
125+
}
126+
}
127+
128+
private static class TestMdcEcsFormatter extends EcsFormatter {
129+
private final Map<String, String> mdc;
130+
131+
public TestMdcEcsFormatter(Map<String, String> mdc) {
132+
this.mdc = mdc;
133+
}
134+
135+
@Override
136+
protected Map<String, String> getMdcEntries() {
137+
return mdc;
138+
}
139+
}
140+
108141
}

jul-ecs-formatter/src/test/java/co/elastic/logging/jul/JulLoggingTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ public void debug(String message) {
8282
logger.log(Level.FINE, message);
8383
}
8484

85-
@Override
86-
public boolean putMdc(String key, String value) {
87-
JulMdc.put(key, value);
88-
return true;
89-
}
90-
9185
@Override
9286
public ParameterizedLogSupport getParameterizedLogSettings() {
9387
return ParameterizedLogSupport.NUMBER_AND_BRACKETS;

jul-ecs-formatter/src/test/java/co/elastic/logging/jul/JulMdcTest.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)