|
1 |
| -package ch.qos.logback.classic.encoder;public class JsonEncoder { |
| 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.spi.ILoggingEvent; |
| 19 | +import ch.qos.logback.core.encoder.EncoderBase; |
| 20 | +import ch.qos.logback.core.util.DirectJson; |
| 21 | + |
| 22 | +import java.nio.charset.Charset; |
| 23 | + |
| 24 | +import static ch.qos.logback.core.CoreConstants.COLON_CHAR; |
| 25 | +import static ch.qos.logback.core.CoreConstants.COMMA_CHAR; |
| 26 | +import static ch.qos.logback.core.CoreConstants.DOUBLE_QUOTE_CHAR; |
| 27 | +import static ch.qos.logback.core.CoreConstants.UTF_8_CHARSET; |
| 28 | +import static ch.qos.logback.core.model.ModelConstants.NULL_STR; |
| 29 | + |
| 30 | +/** |
| 31 | + * |
| 32 | + * |
| 33 | + */ |
| 34 | +public class JsonEncoder extends EncoderBase<ILoggingEvent> { |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + static int DEFAULT_SIZE = 1024; |
| 39 | + static int DEFAULT_SIZE_WITH_THROWABLE = DEFAULT_SIZE*8; |
| 40 | + |
| 41 | + static byte[] EMPTY_BYTES = new byte[0]; |
| 42 | + |
| 43 | + |
| 44 | + public static final String CONTEXT_ATTR_NAME = "context"; |
| 45 | + public static final String TIMESTAMP_ATTR_NAME = "timestamp"; |
| 46 | + public static final String LEVEL_ATTR_NAME = "level"; |
| 47 | + public static final String MARKERS_ATTR_NAME = "markers"; |
| 48 | + public static final String THREAD_ATTR_NAME = "thread"; |
| 49 | + public static final String MDC_ATTR_NAME = "mdc"; |
| 50 | + public static final String LOGGER_ATTR_NAME = "logger"; |
| 51 | + public static final String MESSAGE_ATTR_NAME = "raw-message"; |
| 52 | + public static final String THROWABLE_ATTR_NAME = "throwable"; |
| 53 | + |
| 54 | + private static final char OPEN_OBJ = '{'; |
| 55 | + private static final char CLOSE_OBJ = '}'; |
| 56 | + private static final char OPEN_ARR = '['; |
| 57 | + private static final char CLOSE_ARR = ']'; |
| 58 | + |
| 59 | + private static final char QUOTE = DOUBLE_QUOTE_CHAR; |
| 60 | + private static final char SP = ' '; |
| 61 | + private static final char ENTRY_SEPARATOR = COLON_CHAR; |
| 62 | + private static final char VALUE_SEPARATOR = COMMA_CHAR; |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + @Override |
| 67 | + public byte[] headerBytes() { |
| 68 | + return EMPTY_BYTES; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public byte[] encode(ILoggingEvent event) { |
| 73 | + final int initialCapacity = event.getThrowableProxy() == null ? DEFAULT_SIZE: DEFAULT_SIZE_WITH_THROWABLE; |
| 74 | + StringBuilder sb = new StringBuilder(initialCapacity); |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + return sb.toString().getBytes(UTF_8_CHARSET); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + public void writeLevel(StringBuilder sb, Level level) { |
| 83 | + String levelString = level != null? level.toString() : NULL_STR; |
| 84 | + writeStringValue(sb, LEVEL_ATTR_NAME, levelString); |
| 85 | + } |
| 86 | + |
| 87 | + void writeStringValue(StringBuilder sb, String attrName, String value) { |
| 88 | + sb.append(attrName).append(ENTRY_SEPARATOR).append(SP).append(QUOTE).append(value); |
| 89 | + Character c = ' '; |
| 90 | + } |
| 91 | + |
| 92 | + public void writeSep(StringBuilder sb) { |
| 93 | + sb.append(','); |
| 94 | + } |
| 95 | + @Override |
| 96 | + public byte[] footerBytes() { |
| 97 | + return EMPTY_BYTES; |
| 98 | + } |
2 | 99 | }
|
0 commit comments