Skip to content

Commit 8b63d3f

Browse files
committed
Merge commit '23b2453b8'
2 parents 1e0827b + 23b2453 commit 8b63d3f

File tree

4 files changed

+82
-14
lines changed

4 files changed

+82
-14
lines changed

logback-classic/src/main/java/ch/qos/logback/classic/encoder/JsonEncoder.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ch.qos.logback.classic.encoder;
22

33
import ch.qos.logback.classic.spi.ILoggingEvent;
4+
import ch.qos.logback.core.encoder.JsonEncoderBase;
5+
import ch.qos.logback.core.util.DirectJson;
46

57
import java.util.ArrayList;
68
import java.util.List;
@@ -11,7 +13,7 @@
1113
*
1214
* @author Henry John Kupty
1315
*/
14-
public class JsonEncoder extends ch.qos.logback.core.encoder.JsonEncoder<ILoggingEvent> {
16+
public class JsonEncoder extends JsonEncoderBase<ILoggingEvent> {
1517

1618

1719
// Excerpt below imported from
@@ -52,28 +54,31 @@ public JsonEncoder() {
5254
this.includeContextName = true;
5355
}
5456

55-
public void writeMessage(ILoggingEvent event) {
57+
//protected = new DirectJson();
58+
59+
60+
public void writeMessage(DirectJson jsonWriter, ILoggingEvent event) {
5661
jsonWriter.writeStringValue(MESSAGE_ATTR_NAME, event.getMessage());
5762
}
5863

59-
public void writeFormattedMessage(ILoggingEvent event) {
64+
public void writeFormattedMessage(DirectJson jsonWriter, ILoggingEvent event) {
6065
jsonWriter.writeStringValue(FORMATTED_MESSAGE_ATTR_NAME, event.getFormattedMessage());
6166
}
6267

63-
public void writeLogger(ILoggingEvent event) {
68+
public void writeLogger(DirectJson jsonWriter, ILoggingEvent event) {
6469
jsonWriter.writeStringValue(LOGGER_ATTR_NAME, event.getLoggerName());
6570
}
6671

67-
public void writeThreadName(ILoggingEvent event) {
72+
public void writeThreadName(DirectJson jsonWriter, ILoggingEvent event) {
6873
jsonWriter.writeStringValue(THREAD_ATTR_NAME, event.getThreadName());
6974
}
7075

71-
public void writeLevel(ILoggingEvent event) {
76+
public void writeLevel(DirectJson jsonWriter, ILoggingEvent event) {
7277
jsonWriter.writeStringValue(LEVEL_ATTR_NAME, event.getLevel().levelStr);
7378
}
7479

7580

76-
public void writeMarkers(ILoggingEvent event) {
81+
public void writeMarkers(DirectJson jsonWriter, ILoggingEvent event) {
7782
var markers = event.getMarkerList();
7883
if (!markers.isEmpty()) {
7984
jsonWriter.openArray(MARKERS_ATTR_NAME);
@@ -87,7 +92,7 @@ public void writeMarkers(ILoggingEvent event) {
8792
}
8893
}
8994

90-
public void writeMdc(ILoggingEvent event) {
95+
public void writeMdc(DirectJson jsonWriter, ILoggingEvent event) {
9196
var mdc = event.getMDCPropertyMap();
9297
if (!mdc.isEmpty()) {
9398
jsonWriter.openObject(MDC_ATTR_NAME);
@@ -122,10 +127,11 @@ public byte[] encode(ILoggingEvent event) {
122127
if (emitters.isEmpty()) {
123128
buildEmitterList();
124129
}
130+
DirectJson jsonWriter = new DirectJson();
125131
jsonWriter.openObject();
126132

127133
for (var emitter: emitters) {
128-
emitter.write(event);
134+
emitter.write(jsonWriter, event);
129135
}
130136

131137
jsonWriter.closeObject();
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2023, QOS.ch. All rights reserved.
4+
*
5+
* This program and the accompanying materials are dual-licensed under
6+
* either the terms of the Eclipse Public License v1.0 as published by
7+
* the Eclipse Foundation
8+
*
9+
* or (per the licensee's choosing)
10+
*
11+
* under the terms of the GNU Lesser General Public License version 2.1
12+
* as published by the Free Software Foundation.
13+
*/
14+
15+
package ch.qos.logback.classic.encoder;
16+
17+
import ch.qos.logback.classic.Level;
18+
import ch.qos.logback.classic.Logger;
19+
import ch.qos.logback.classic.LoggerContext;
20+
import ch.qos.logback.classic.spi.ILoggingEvent;
21+
import ch.qos.logback.classic.spi.LoggingEvent;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Disabled;
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.IOException;
28+
import java.nio.charset.Charset;
29+
30+
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
32+
33+
34+
@Disabled
35+
public class JsonEncoderTest {
36+
37+
38+
39+
LoggerContext context = new LoggerContext();
40+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
41+
Logger logger = context.getLogger(PatternLayoutEncoderTest.class);
42+
Charset utf8Charset = Charset.forName("UTF-8");
43+
44+
JsonEncoder je = new JsonEncoder();
45+
46+
@BeforeEach
47+
public void setUp() {
48+
je.setContext(context);
49+
}
50+
51+
@Test
52+
public void smoke() throws IOException {
53+
String msg = "hello";
54+
ILoggingEvent event = makeLoggingEvent(msg);
55+
byte[] eventBytes = je.encode(event);
56+
baos.write(eventBytes);
57+
assertEquals(msg, baos.toString());
58+
}
59+
60+
ILoggingEvent makeLoggingEvent(String message) {
61+
return new LoggingEvent("", logger, Level.DEBUG, message, null, null);
62+
}
63+
64+
}

logback-core/src/main/java/ch/qos/logback/core/encoder/JsonEncoder.java renamed to logback-core/src/main/java/ch/qos/logback/core/encoder/JsonEncoderBase.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@
1010
* @param <E>
1111
* @author Henry John Kupty
1212
*/
13-
public abstract class JsonEncoder<E> extends EncoderBase<E> {
13+
public abstract class JsonEncoderBase<E> extends EncoderBase<E> {
1414
@FunctionalInterface
1515
protected interface Emitter<E> {
16-
void write(E event);
16+
void write(DirectJson jsonWriter, E event);
1717
}
1818

1919
private static final byte[] LINE_BREAK = System.getProperty("line.separator").getBytes(StandardCharsets.UTF_8);
2020

21-
protected DirectJson jsonWriter = new DirectJson();
22-
2321
@Override
2422
public byte[] headerBytes() {
2523
return LINE_BREAK;

logback-core/src/main/java/ch/qos/logback/core/util/DirectJson.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public byte[] flush() {
238238
byte[] result = new byte[buffer.position()];
239239
buffer.flip();
240240
buffer.get(result);
241-
buffer.reset();
241+
buffer.clear();
242242

243243
return result;
244244
}

0 commit comments

Comments
 (0)