Skip to content

Commit

Permalink
ongoing work on JsonEncoder
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Apr 16, 2023
1 parent c2937fc commit 23b2453
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ch.qos.logback.classic.encoder;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.encoder.JsonEncoderBase;
import ch.qos.logback.core.util.DirectJson;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -11,7 +13,7 @@
*
* @author Henry John Kupty
*/
public class JsonEncoder extends ch.qos.logback.core.encoder.JsonEncoder<ILoggingEvent> {
public class JsonEncoder extends JsonEncoderBase<ILoggingEvent> {


// Excerpt below imported from
Expand Down Expand Up @@ -52,28 +54,31 @@ public JsonEncoder() {
this.includeContextName = true;
}

public void writeMessage(ILoggingEvent event) {
//protected = new DirectJson();


public void writeMessage(DirectJson jsonWriter, ILoggingEvent event) {
jsonWriter.writeStringValue(MESSAGE_ATTR_NAME, event.getMessage());
}

public void writeFormattedMessage(ILoggingEvent event) {
public void writeFormattedMessage(DirectJson jsonWriter, ILoggingEvent event) {
jsonWriter.writeStringValue(FORMATTED_MESSAGE_ATTR_NAME, event.getFormattedMessage());
}

public void writeLogger(ILoggingEvent event) {
public void writeLogger(DirectJson jsonWriter, ILoggingEvent event) {
jsonWriter.writeStringValue(LOGGER_ATTR_NAME, event.getLoggerName());
}

public void writeThreadName(ILoggingEvent event) {
public void writeThreadName(DirectJson jsonWriter, ILoggingEvent event) {
jsonWriter.writeStringValue(THREAD_ATTR_NAME, event.getThreadName());
}

public void writeLevel(ILoggingEvent event) {
public void writeLevel(DirectJson jsonWriter, ILoggingEvent event) {
jsonWriter.writeStringValue(LEVEL_ATTR_NAME, event.getLevel().levelStr);
}


public void writeMarkers(ILoggingEvent event) {
public void writeMarkers(DirectJson jsonWriter, ILoggingEvent event) {
var markers = event.getMarkerList();
if (!markers.isEmpty()) {
jsonWriter.openArray(MARKERS_ATTR_NAME);
Expand All @@ -87,7 +92,7 @@ public void writeMarkers(ILoggingEvent event) {
}
}

public void writeMdc(ILoggingEvent event) {
public void writeMdc(DirectJson jsonWriter, ILoggingEvent event) {
var mdc = event.getMDCPropertyMap();
if (!mdc.isEmpty()) {
jsonWriter.openObject(MDC_ATTR_NAME);
Expand Down Expand Up @@ -122,10 +127,11 @@ public byte[] encode(ILoggingEvent event) {
if (emitters.isEmpty()) {
buildEmitterList();
}
DirectJson jsonWriter = new DirectJson();
jsonWriter.openObject();

for (var emitter: emitters) {
emitter.write(event);
emitter.write(jsonWriter, event);
}

jsonWriter.closeObject();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2023, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/

package ch.qos.logback.classic.encoder;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggingEvent;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;

import static org.junit.jupiter.api.Assertions.assertEquals;



@Disabled
public class JsonEncoderTest {



LoggerContext context = new LoggerContext();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Logger logger = context.getLogger(PatternLayoutEncoderTest.class);
Charset utf8Charset = Charset.forName("UTF-8");

JsonEncoder je = new JsonEncoder();

@BeforeEach
public void setUp() {
je.setContext(context);
}

@Test
public void smoke() throws IOException {
String msg = "hello";
ILoggingEvent event = makeLoggingEvent(msg);
byte[] eventBytes = je.encode(event);
baos.write(eventBytes);
assertEquals(msg, baos.toString());
}

ILoggingEvent makeLoggingEvent(String message) {
return new LoggingEvent("", logger, Level.DEBUG, message, null, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@
* @param <E>
* @author Henry John Kupty
*/
public abstract class JsonEncoder<E> extends EncoderBase<E> {
public abstract class JsonEncoderBase<E> extends EncoderBase<E> {
@FunctionalInterface
protected interface Emitter<E> {
void write(E event);
void write(DirectJson jsonWriter, E event);
}

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

protected DirectJson jsonWriter = new DirectJson();

@Override
public byte[] headerBytes() {
return LINE_BREAK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public byte[] flush() {
byte[] result = new byte[buffer.position()];
buffer.flip();
buffer.get(result);
buffer.reset();
buffer.clear();

return result;
}
Expand Down

0 comments on commit 23b2453

Please sign in to comment.