Skip to content

Commit bcf0c56

Browse files
update plugin
1 parent 84b3c48 commit bcf0c56

File tree

5 files changed

+54
-75
lines changed

5 files changed

+54
-75
lines changed

logback13/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ tasks.withType<Javadoc> {
4848
}
4949

5050
configure<JavaPluginConvention> {
51-
sourceCompatibility = JavaVersion.VERSION_1_8
52-
targetCompatibility = JavaVersion.VERSION_1_8
51+
sourceCompatibility = JavaVersion.VERSION_17
52+
targetCompatibility = JavaVersion.VERSION_17
5353
}
5454

5555
tasks.register<Jar>("sourcesJar") {

logback13/src/main/java/com/newrelic/logging/logback13/NewRelicAsyncAppender.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public void start() {
4444
super.start();
4545
}
4646

47+
@Override
48+
protected void append(ILoggingEvent eventObject) {
49+
Map<String, String> linkingMetadata = agentSupplier.get().getLinkingMetadata();
50+
if (!isNoOpMDC) {
51+
for (Map.Entry<String, String> entry : linkingMetadata.entrySet()) {
52+
MDC.put(NEW_RELIC_PREFIX + entry.getKey(), entry.getValue());
53+
}
54+
}
55+
super.append(eventObject);
56+
}
57+
4758
@Override
4859
protected void preprocess(ILoggingEvent eventObject) {
4960
Map<String, String> linkingMetadata = agentSupplier.get().getLinkingMetadata();
@@ -52,6 +63,12 @@ protected void preprocess(ILoggingEvent eventObject) {
5263
for (Map.Entry<String, String> entry : linkingMetadata.entrySet()) {
5364
MDC.put(NEW_RELIC_PREFIX + entry.getKey(), entry.getValue());
5465
}
66+
67+
Map<String, String> mdcCopy = MDC.getCopyOfContextMap();
68+
if (mdcCopy != null && eventObject instanceof LoggingEvent) {
69+
((LoggingEvent) eventObject).setMDCPropertyMap(mdcCopy);
70+
}
71+
5572
super.preprocess(eventObject);
5673
/*
5774
* In logback-1.3.x, calling eventObject.getMDCPropertyMap() returns an immutable map (Collections.emptyMap()).

logback13/src/main/java/com/newrelic/logging/logback13/NewRelicEncoder.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import ch.qos.logback.core.encoder.Encoder;
1010
import ch.qos.logback.core.encoder.EncoderBase;
1111

12+
import java.io.IOException;
13+
import java.io.OutputStream;
1214
import java.nio.ByteBuffer;
1315
import java.nio.charset.StandardCharsets;
1416
import java.util.Arrays;
@@ -31,28 +33,20 @@
3133
* @see <a href="https://logback.qos.ch/manual/encoders.html#interface">Logback Encoders</a>
3234
*/
3335
public class NewRelicEncoder extends EncoderBase<ILoggingEvent> {
34-
private NewRelicJsonLayout layout;
35-
36-
private Integer maxStackSize = getMaxStackSize();
36+
private NewRelicJsonLayout layout = new NewRelicJsonLayout();
3737

3838
@Override
3939
public byte[] encode(ILoggingEvent event) {
4040
String laidOutResult = layout.doLayout(event);
4141
return laidOutResult.getBytes(StandardCharsets.UTF_8);
4242
}
4343

44-
4544
@Override
4645
public void start() {
4746
super.start();
48-
layout = new NewRelicJsonLayout(maxStackSize);
4947
layout.start();
5048
}
5149

52-
public void setMaxStackSize(final Integer maxStackSize) {
53-
this.maxStackSize = maxStackSize;
54-
}
55-
5650
@Override
5751
public byte[] headerBytes() {
5852
return new byte[0];

logback13/src/main/java/com/newrelic/logging/logback13/NewRelicJsonLayout.java

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,48 @@
66
package com.newrelic.logging.logback13;
77

88
import ch.qos.logback.classic.spi.ILoggingEvent;
9-
import ch.qos.logback.classic.spi.IThrowableProxy;
10-
import ch.qos.logback.classic.spi.ThrowableProxyUtil;
119
import ch.qos.logback.core.Context;
12-
import ch.qos.logback.core.Layout;
10+
import ch.qos.logback.core.LayoutBase;
1311
import ch.qos.logback.core.status.ErrorStatus;
1412
import ch.qos.logback.core.status.InfoStatus;
1513
import ch.qos.logback.core.status.Status;
1614
import ch.qos.logback.core.status.WarnStatus;
1715
import com.fasterxml.jackson.core.JsonGenerator;
1816
import com.newrelic.logging.core.ElementName;
19-
import com.newrelic.logging.core.ExceptionUtil;
2017
import com.newrelic.logging.core.LogExtensionConfig;
18+
import com.fasterxml.jackson.core.JsonFactory;
2119
import org.slf4j.Marker;
2220

2321
import java.io.IOException;
2422
import java.io.StringWriter;
23+
import java.util.List;
2524
import java.util.Map;
2625

2726
import static com.newrelic.logging.core.LogExtensionConfig.CONTEXT_PREFIX;
28-
import static com.newrelic.logging.core.LogExtensionConfig.getMaxStackSize;
2927
import static com.newrelic.logging.logback13.NewRelicAsyncAppender.NEW_RELIC_PREFIX;
3028

31-
public class NewRelicJsonLayout implements Layout<ILoggingEvent> {
32-
private final Integer maxStackSize;
29+
public class NewRelicJsonLayout extends LayoutBase<ILoggingEvent> {
3330
private boolean started = false;
3431
private Context context;
3532

36-
public NewRelicJsonLayout() {
37-
this(getMaxStackSize());
38-
}
39-
40-
public NewRelicJsonLayout(Integer maxStackSize) {
41-
this.maxStackSize = maxStackSize;
42-
}
43-
4433
@Override
4534
public String doLayout(ILoggingEvent event) {
4635
StringWriter sw = new StringWriter();
4736

48-
try (JsonGenerator generator = JsonFactoryProvider.getInstance().createGenerator(sw)) {
37+
try (JsonGenerator generator = new JsonFactory().createGenerator(sw);) {
4938
writeToGenerator(event, generator);
5039
} catch (Throwable ignored) {
51-
return event.getFormattedMessage() + "\n";
40+
return event.getFormattedMessage();
5241
}
5342

54-
return sw.toString() + "\n";
43+
sw.append('\n');
44+
return sw.toString();
5545
}
5646

5747
private void writeToGenerator(ILoggingEvent event, JsonGenerator generator) throws IOException {
58-
generator.writeStartObject();
48+
System.out.println("[writeToGenerator] Executing layout event for: " + event.getFormattedMessage());
5949

50+
generator.writeStartObject();
6051
generator.writeStringField(ElementName.MESSAGE, event.getFormattedMessage());
6152
generator.writeNumberField(ElementName.TIMESTAMP, event.getTimeStamp());
6253
generator.writeStringField(ElementName.LOG_LEVEL, event.getLevel().toString());
@@ -71,41 +62,30 @@ private void writeToGenerator(ILoggingEvent event, JsonGenerator generator) thro
7162
}
7263

7364
Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();
74-
for (Map.Entry<String, String> entry : mdcPropertyMap.entrySet()) {
75-
if (entry.getValue() != null && !entry.getValue().isEmpty()) {
76-
if (entry.getKey().startsWith(NEW_RELIC_PREFIX)) {
77-
String key = entry.getKey().substring(NEW_RELIC_PREFIX.length());
78-
generator.writeStringField(key, entry.getValue());
79-
} else if (LogExtensionConfig.shouldAddMDC()) {
80-
generator.writeStringField(CONTEXT_PREFIX + entry.getKey(), entry.getValue());
65+
if (mdcPropertyMap != null) {
66+
System.out.println("[writeToGenerator] mdcPropertyMap is not null, size: " + mdcPropertyMap.size());
67+
for (Map.Entry<String, String> entry : mdcPropertyMap.entrySet()) {
68+
if (entry.getValue() != null && !entry.getValue().isEmpty()) {
69+
System.out.println("[writeToGenerator] Current entry key: " + entry.getKey());
70+
if (entry.getKey().startsWith(NEW_RELIC_PREFIX)) {
71+
String key = entry.getKey().substring(NEW_RELIC_PREFIX.length());
72+
generator.writeStringField(key, entry.getValue());
73+
} else if (LogExtensionConfig.shouldAddMDC()){
74+
generator.writeStringField(CONTEXT_PREFIX + entry.getKey(), entry.getValue());
75+
}
8176
}
8277
}
78+
} else {
79+
System.out.println("[writeToGenerator] mdcPropertyMap is null");
8380
}
8481

85-
if (event.getMarkerList() != null && !event.getMarkerList().isEmpty()) {
82+
List<Marker> markerList = event.getMarkerList();
83+
if (markerList != null && !markerList.isEmpty()) {
8684
generator.writeArrayFieldStart(ElementName.MARKER);
87-
for (Marker marker : event.getMarkerList()) {
85+
for (Marker marker : markerList) {
8886
generator.writeString(marker.getName());
8987
}
9088
generator.writeEndArray();
91-
92-
}
93-
94-
Object[] customArgumentArray = event.getArgumentArray();
95-
if (customArgumentArray != null) {
96-
for (Object oneCustomArgumentObject : customArgumentArray) {
97-
if (oneCustomArgumentObject instanceof CustomArgument) {
98-
CustomArgument customArgument = (CustomArgument) oneCustomArgumentObject;
99-
generator.writeStringField(customArgument.getKey(), customArgument.getValue());
100-
}
101-
}
102-
}
103-
104-
IThrowableProxy proxy = event.getThrowableProxy();
105-
if (proxy != null) {
106-
generator.writeObjectField(ElementName.ERROR_CLASS, proxy.getClassName());
107-
generator.writeObjectField(ElementName.ERROR_MESSAGE, proxy.getMessage());
108-
generator.writeObjectField(ElementName.ERROR_STACK, ExceptionUtil.transformLogbackStackTraceString(ThrowableProxyUtil.asString(proxy)));
10989
}
11090

11191
generator.writeEndObject();

logback13/src/test/java/NewRelicLogback13Tests.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private void givenALoggingEvent() {
176176

177177
private void givenALoggingEventWithExceptionData() {
178178
givenALoggingEvent();
179-
event.setThrowableProxy(new ThrowableProxy(new Exception("~~ oops ~~")));
179+
event.setThrowableProxy(new ThrowableProxy(new Exception("some interesting info")));
180180
}
181181

182182
private void givenALoggingEventWithCallerData() {
@@ -241,10 +241,6 @@ private void whenTheEventIsAppended() throws IOException {
241241
outputStream.flush();
242242
}
243243

244-
private boolean appenderIsIdle() {
245-
return ((NewRelicAsyncAppender) appender).getQueueSize() == 0;
246-
}
247-
248244
private void thenJsonLayoutWasUsed() throws IOException {
249245
LogAsserts.assertFieldValues(
250246
getOutput(),
@@ -257,17 +253,10 @@ private void thenJsonLayoutWasUsed() throws IOException {
257253
}
258254

259255
private void thenMockAgentDataIsInTheMessage() throws Throwable {
260-
String output = getOutput();
261-
262-
System.out.println("ED: OUTPUT CHARS: ");
263-
for (char c : output.toCharArray()) {
264-
System.out.print((int) c + " ");
265-
}
266-
267256
assertTrue(
268-
output.contains("\"some.key\"=\"some.value\"")
269-
|| output.contains("\"some.key\":\"some.value\""),
270-
"Expected log output to contain linking metadata: " + output
257+
getOutput().contains("some.key=some.value")
258+
|| getOutput().contains("\"some.key\":\"some.value\""),
259+
"Expected >>" + getOutput() + "<< to contain some.key to some.value"
271260
);
272261
}
273262

@@ -284,7 +273,7 @@ private void thenTheExceptionDataIsInTheMessage() throws Throwable {
284273
ImmutableMap.of(
285274
"error.class", "java.lang.Exception",
286275
"error.stack", Pattern.compile(".*NewRelicLogback13Tests\\.shouldAppendErrorDataCorrectly.*", Pattern.DOTALL),
287-
"error.message", "~~ oops ~~")
276+
"error.message", "some error message")
288277
);
289278
}
290279

@@ -322,10 +311,9 @@ private void thenTheMDCFieldsAreInTheMessage(boolean shouldExist) throws Throwab
322311
private String getOutput() throws IOException {
323312
if (output == null) {
324313
output = bufferedReader.readLine() + "\n";
325-
System.out.println("Output: " + output);
326314
appender.stop();
327315
}
328-
// assertNotNull(output);
316+
assertNotNull(output);
329317
return output;
330318
}
331319

0 commit comments

Comments
 (0)