Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify + Allow custom MDC lookup #193

Merged
merged 3 commits into from
Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -29,6 +29,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.Formatter;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
Expand Down Expand Up @@ -69,7 +70,7 @@ public String format(final LogRecord record) {
EcsJsonSerializer.serializeFormattedMessage(builder, super.formatMessage(record));
EcsJsonSerializer.serializeEcsVersion(builder);
EcsJsonSerializer.serializeAdditionalFields(builder, additionalFields);
EcsJsonSerializer.serializeMDC(builder, JulMdc.getEntries());
EcsJsonSerializer.serializeMDC(builder, getMdcEntries());
EcsJsonSerializer.serializeServiceName(builder, serviceName);
EcsJsonSerializer.serializeServiceVersion(builder, serviceVersion);
EcsJsonSerializer.serializeServiceEnvironment(builder, serviceEnvironment);
Expand All @@ -92,6 +93,16 @@ public String format(final LogRecord record) {
return builder.toString();
}

/**
* Used by APM agent to provide MDC for JUL through instrumentation and to allow testing without an actual MDC
* implementation.
*
* @return MDC entries
*/
protected Map<String, String> getMdcEntries() {
return Collections.emptyMap();
}

public void setIncludeOrigin(final boolean includeOrigin) {
this.includeOrigin = includeOrigin;
}
Expand Down
71 changes: 0 additions & 71 deletions jul-ecs-formatter/src/main/java/co/elastic/logging/jul/JulMdc.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Expand All @@ -24,12 +24,15 @@
*/
package co.elastic.logging.jul;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.LogRecord;

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

private final LogRecord record = new LogRecord(Level.INFO, "Example Message");
private final ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectMapper objectMapper = new ObjectMapper();

@BeforeEach
void setUp() {
Expand All @@ -57,21 +60,21 @@ public void testFormatWithIncludeOriginFlag() throws Exception {

final String result = formatter.format(record);

assertThat(objectMapper.readTree(result).at("/log/origin/file/name").textValue()).isEqualTo("ExampleClass.java");
assertThat(objectMapper.readTree(result).at("/log/origin/function").textValue()).isEqualTo("exampleMethod");
assertThat(parseJson(result).at("/log/origin/file/name").textValue()).isEqualTo("ExampleClass.java");
assertThat(parseJson(result).at("/log/origin/function").textValue()).isEqualTo("exampleMethod");
}

@Test
public void testFormatWithoutIncludeOriginFlag() throws Exception {
final JsonNode result = objectMapper.readTree(formatter.format(record));
final JsonNode result = parseJson(formatter.format(record));
assertThat(result.get("log.origin")).isNull();
}

@Test
public void testFormatWithoutLoggerName() throws Exception {
record.setLoggerName(null);

final JsonNode result = objectMapper.readTree(formatter.format(record));
final JsonNode result = parseJson(formatter.format(record));

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

final JsonNode result = objectMapper.readTree(formatter.format(record));
final JsonNode result = parseJson(formatter.format(record));

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

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

JsonNode result = objectMapper.readTree(formatter.format(record));
JsonNode result = parseJson(formatter.format(record));
assertThat(result.at("/log/origin/file/name").textValue()).isEqualTo("<Unknown>");
assertThat(result.at("/log/origin/function").textValue()).isEqualTo("exampleMethod");
}

@Test
void testMdcSerialization_singleEntry() {
Map<String,String> mdc = new HashMap<>();
TestMdcEcsFormatter mdcFormatter = new TestMdcEcsFormatter(mdc);
mdc.put("mdc.key", "value");
JsonNode result = parseJson(mdcFormatter.format(record));
assertThat(result.get("mdc.key").textValue()).isEqualTo("value");
}

private static JsonNode parseJson(String formatter) {
try {
return objectMapper.readTree(formatter);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

private static class TestMdcEcsFormatter extends EcsFormatter {
private final Map<String, String> mdc;

public TestMdcEcsFormatter(Map<String, String> mdc) {
this.mdc = mdc;
}

@Override
protected Map<String, String> getMdcEntries() {
return mdc;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ public void debug(String message) {
logger.log(Level.FINE, message);
}

@Override
public boolean putMdc(String key, String value) {
JulMdc.put(key, value);
return true;
}

@Override
public ParameterizedLogSupport getParameterizedLogSettings() {
return ParameterizedLogSupport.NUMBER_AND_BRACKETS;
Expand Down

This file was deleted.