From f93f3bbe643d3859cc18cb4ee8829d9c26e8d357 Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Mon, 1 May 2023 22:16:23 +0200 Subject: [PATCH] remove older JsonEncoder Signed-off-by: Ceki Gulcu --- .../logback/classic/encoder/JsonEncoder.java | 99 ++++++++++++- .../logback/classic/encoder/JsonEncoder2.java | 140 ------------------ .../classic/encoder/JsonEncoder2Test.java | 84 ----------- .../core/encoder/JsonEncoderBase2.java | 30 ---- 4 files changed, 98 insertions(+), 255 deletions(-) delete mode 100644 logback-classic/src/main/java/ch/qos/logback/classic/encoder/JsonEncoder2.java delete mode 100644 logback-classic/src/test/java/ch/qos/logback/classic/encoder/JsonEncoder2Test.java delete mode 100644 logback-core/src/main/java/ch/qos/logback/core/encoder/JsonEncoderBase2.java diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/encoder/JsonEncoder.java b/logback-classic/src/main/java/ch/qos/logback/classic/encoder/JsonEncoder.java index 68f7c6e37c..1e70d485cc 100644 --- a/logback-classic/src/main/java/ch/qos/logback/classic/encoder/JsonEncoder.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/encoder/JsonEncoder.java @@ -1,2 +1,99 @@ -package ch.qos.logback.classic.encoder;public class JsonEncoder { +/* + * 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.spi.ILoggingEvent; +import ch.qos.logback.core.encoder.EncoderBase; +import ch.qos.logback.core.util.DirectJson; + +import java.nio.charset.Charset; + +import static ch.qos.logback.core.CoreConstants.COLON_CHAR; +import static ch.qos.logback.core.CoreConstants.COMMA_CHAR; +import static ch.qos.logback.core.CoreConstants.DOUBLE_QUOTE_CHAR; +import static ch.qos.logback.core.CoreConstants.UTF_8_CHARSET; +import static ch.qos.logback.core.model.ModelConstants.NULL_STR; + +/** + * + * + */ +public class JsonEncoder extends EncoderBase { + + + + static int DEFAULT_SIZE = 1024; + static int DEFAULT_SIZE_WITH_THROWABLE = DEFAULT_SIZE*8; + + static byte[] EMPTY_BYTES = new byte[0]; + + + public static final String CONTEXT_ATTR_NAME = "context"; + public static final String TIMESTAMP_ATTR_NAME = "timestamp"; + public static final String LEVEL_ATTR_NAME = "level"; + public static final String MARKERS_ATTR_NAME = "markers"; + public static final String THREAD_ATTR_NAME = "thread"; + public static final String MDC_ATTR_NAME = "mdc"; + public static final String LOGGER_ATTR_NAME = "logger"; + public static final String MESSAGE_ATTR_NAME = "raw-message"; + public static final String THROWABLE_ATTR_NAME = "throwable"; + + private static final char OPEN_OBJ = '{'; + private static final char CLOSE_OBJ = '}'; + private static final char OPEN_ARR = '['; + private static final char CLOSE_ARR = ']'; + + private static final char QUOTE = DOUBLE_QUOTE_CHAR; + private static final char SP = ' '; + private static final char ENTRY_SEPARATOR = COLON_CHAR; + private static final char VALUE_SEPARATOR = COMMA_CHAR; + + + + @Override + public byte[] headerBytes() { + return EMPTY_BYTES; + } + + @Override + public byte[] encode(ILoggingEvent event) { + final int initialCapacity = event.getThrowableProxy() == null ? DEFAULT_SIZE: DEFAULT_SIZE_WITH_THROWABLE; + StringBuilder sb = new StringBuilder(initialCapacity); + + + + return sb.toString().getBytes(UTF_8_CHARSET); + } + + + public void writeLevel(StringBuilder sb, Level level) { + String levelString = level != null? level.toString() : NULL_STR; + writeStringValue(sb, LEVEL_ATTR_NAME, levelString); + } + + void writeStringValue(StringBuilder sb, String attrName, String value) { + sb.append(attrName).append(ENTRY_SEPARATOR).append(SP).append(QUOTE).append(value); + Character c = ' '; + } + + public void writeSep(StringBuilder sb) { + sb.append(','); + } + @Override + public byte[] footerBytes() { + return EMPTY_BYTES; + } } diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/encoder/JsonEncoder2.java b/logback-classic/src/main/java/ch/qos/logback/classic/encoder/JsonEncoder2.java deleted file mode 100644 index 4bfd5bfff7..0000000000 --- a/logback-classic/src/main/java/ch/qos/logback/classic/encoder/JsonEncoder2.java +++ /dev/null @@ -1,140 +0,0 @@ -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; - -/** - * This is a concrete JsonEncoder for {@link ILoggingEvent} that emits fields according to object's configuration. - * It is partially imported from penna, but adapted to logback's structure. - * - * @author Henry John Kupty - */ -public class JsonEncoder extends JsonEncoderBase { - - - // Excerpt below imported from - // ch.qos.logback.contrib.json.classic.JsonLayout - public static final String TIMESTAMP_ATTR_NAME = "timestamp"; - public static final String LEVEL_ATTR_NAME = "level"; - public static final String MARKERS_ATTR_NAME = "tags"; - public static final String THREAD_ATTR_NAME = "thread"; - public static final String MDC_ATTR_NAME = "mdc"; - public static final String LOGGER_ATTR_NAME = "logger"; - public static final String FORMATTED_MESSAGE_ATTR_NAME = "message"; - public static final String MESSAGE_ATTR_NAME = "raw-message"; - public static final String EXCEPTION_ATTR_NAME = "exception"; - public static final String CONTEXT_ATTR_NAME = "context"; - - protected boolean includeLevel; - protected boolean includeThreadName; - protected boolean includeMDC; - protected boolean includeLoggerName; - protected boolean includeFormattedMessage; - protected boolean includeMessage; - protected boolean includeException; - protected boolean includeContextName; - - private final List> emitters; - - - public JsonEncoder() { - super(); - - emitters = new ArrayList<>(); - this.includeLevel = true; - this.includeThreadName = true; - this.includeMDC = true; - this.includeLoggerName = true; - this.includeFormattedMessage = true; - this.includeException = true; - this.includeContextName = true; - } - - //protected = new DirectJson(); - - - public void writeMessage(DirectJson jsonWriter, ILoggingEvent event) { - jsonWriter.writeStringValue(MESSAGE_ATTR_NAME, event.getMessage()); - } - - public void writeFormattedMessage(DirectJson jsonWriter, ILoggingEvent event) { - jsonWriter.writeStringValue(FORMATTED_MESSAGE_ATTR_NAME, event.getFormattedMessage()); - } - - public void writeLogger(DirectJson jsonWriter, ILoggingEvent event) { - jsonWriter.writeStringValue(LOGGER_ATTR_NAME, event.getLoggerName()); - } - - public void writeThreadName(DirectJson jsonWriter, ILoggingEvent event) { - jsonWriter.writeStringValue(THREAD_ATTR_NAME, event.getThreadName()); - } - - public void writeLevel(DirectJson jsonWriter, ILoggingEvent event) { - jsonWriter.writeStringValue(LEVEL_ATTR_NAME, event.getLevel().levelStr); - } - - - public void writeMarkers(DirectJson jsonWriter, ILoggingEvent event) { - var markers = event.getMarkerList(); - if (!markers.isEmpty()) { - jsonWriter.openArray(MARKERS_ATTR_NAME); - for (var marker : markers) { - jsonWriter.writeString(marker.getName()); - jsonWriter.writeSep(); - } - // Close array will overwrite the last "," in the buffer, so we are OK - jsonWriter.closeArray(); - jsonWriter.writeSep(); - } - } - - public void writeMdc(DirectJson jsonWriter, ILoggingEvent event) { - var mdc = event.getMDCPropertyMap(); - if (!mdc.isEmpty()) { - jsonWriter.openObject(MDC_ATTR_NAME); - for (var entry : mdc.entrySet()) { - jsonWriter.writeStringValue(entry.getKey(), entry.getValue()); - } - jsonWriter.closeObject(); - jsonWriter.writeSep(); - } - } - - private void buildEmitterList() { - // This method should be re-entrant and allow for reconfiguring the emitters if something change; - emitters.clear(); - - // TODO figure out order - if (includeLevel) emitters.add(this::writeLevel); - if (includeMDC) emitters.add(this::writeMdc); - if (includeMessage) emitters.add(this::writeMessage); - if (includeFormattedMessage) emitters.add(this::writeFormattedMessage); - if (includeThreadName) emitters.add(this::writeThreadName); - if (includeLoggerName) emitters.add(this::writeLogger); - // TODO add fields missing: - // context - // exception - // custom data - // marker - } - - @Override - public byte[] encode(ILoggingEvent event) { - if (emitters.isEmpty()) { - buildEmitterList(); - } - DirectJson jsonWriter = new DirectJson(); - jsonWriter.openObject(); - - for (var emitter: emitters) { - emitter.write(jsonWriter, event); - } - - jsonWriter.closeObject(); - return jsonWriter.flush(); - } -} diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/encoder/JsonEncoder2Test.java b/logback-classic/src/test/java/ch/qos/logback/classic/encoder/JsonEncoder2Test.java deleted file mode 100644 index c4a8b814a5..0000000000 --- a/logback-classic/src/test/java/ch/qos/logback/classic/encoder/JsonEncoder2Test.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * 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; - -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); - String witnessPattern = makeWitness(event); - assertEquals(witnessPattern, baos.toString()); - } - - @Test - public void twoEvents() throws IOException { - - ILoggingEvent event0 = makeLoggingEvent("hello"); - ILoggingEvent event1 = makeLoggingEvent("world"); - - byte[] eventBytes0 = je.encode(event0); - byte[] eventBytes1 = je.encode(event1); - - baos.write(eventBytes0); - baos.write(eventBytes1); - - String witnessPattern0 = makeWitness(event0); - String witnessPattern1 = makeWitness(event1); - - assertEquals(witnessPattern0+witnessPattern1, baos.toString()); - } - - - private static String makeWitness(ILoggingEvent event) { - return "{\"level\":\"" + event.getLevel() + "\",\"message\":\"" + event.getMessage() + "\",\"thread\":\"" - + event.getThreadName() + "\",\"logger\":\"" + event.getLoggerName() + "\"}"; - } - - ILoggingEvent makeLoggingEvent(String message) { - return new LoggingEvent("", logger, Level.DEBUG, message, null, null); - } - -} diff --git a/logback-core/src/main/java/ch/qos/logback/core/encoder/JsonEncoderBase2.java b/logback-core/src/main/java/ch/qos/logback/core/encoder/JsonEncoderBase2.java deleted file mode 100644 index 29ff2a18dc..0000000000 --- a/logback-core/src/main/java/ch/qos/logback/core/encoder/JsonEncoderBase2.java +++ /dev/null @@ -1,30 +0,0 @@ -package ch.qos.logback.core.encoder; - -import ch.qos.logback.core.util.DirectJson; - -import java.nio.charset.StandardCharsets; - -/** - * This class allows for concrete encoders to write json log messages. - * - * @param - * @author Henry John Kupty - */ -public abstract class JsonEncoderBase extends EncoderBase { - @FunctionalInterface - protected interface Emitter { - void write(DirectJson jsonWriter, E event); - } - - private static final byte[] LINE_BREAK = System.getProperty("line.separator").getBytes(StandardCharsets.UTF_8); - - @Override - public byte[] headerBytes() { - return LINE_BREAK; - } - - @Override - public byte[] footerBytes() { - return new byte[0]; - } -}