View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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  package ch.qos.logback.classic.kryo;
15  
16  import ch.qos.logback.classic.spi.LoggerContextVO;
17  import ch.qos.logback.classic.spi.ThrowableProxyVO;
18  import ch.qos.logback.core.kryo.SerializerBase;
19  import ch.qos.logback.core.kryo.SerializerBase.BiMapType;
20  import com.esotericsoftware.kryo.kryo5.Kryo;
21  import com.esotericsoftware.kryo.kryo5.Serializer;
22  import com.esotericsoftware.kryo.kryo5.io.Input;
23  import com.esotericsoftware.kryo.kryo5.io.Output;
24  import com.esotericsoftware.kryo.kryo5.serializers.CollectionSerializer;
25  import org.slf4j.Marker;
26  import org.slf4j.event.KeyValuePair;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * Stream version 5 event serializer.
34   *
35   * <p>Logger name, level and thread name are followed by a 1-byte presence bitmap for
36   * message, arguments, markers, key-value pairs, MDC, throwable and {@code callerData}
37   * (usually absent). Message may be null (fluent {@code .log()} with no message).
38   * Present fields are written with {@code writeObject} (no per-field null byte).
39   * Timestamp and nanoseconds are one {@link LongDeltaSerializer} field:
40   * {@code (epochSecond << 30) | nanos}. Caller data follows throwable when present.
41   *
42   * <p>Long unique strings (message, string arguments, KVP string values and MDC values)
43   * share one {@link MultiEventCompressedStringSerializer} so there is a single deflate
44   * stream. Short repeating strings (thread name, MDC keys, KVP keys) use
45   * {@link BasicStringSerializer} and the id cache.
46   *
47   * <p>Logger names and stack-frame class names (caller data and throwable frames) use
48   * {@link BasicStringSerializer}. Each of the three has its own instance.
49   *
50   * <p>Selected when the stream header version is
51   * {@link KryoClassicConstants#STREAM_VERSION_5}.
52   */
53  public class LoggingEventKryoVOSerializerV5 extends Serializer<LoggingEventKryoVO> implements FieldByteAccumulatorAware {
54  
55      static final int PRESENT_MESSAGE = 1 << 0;
56      static final int PRESENT_ARGUMENTS = 1 << 1;
57      static final int PRESENT_MARKERS = 1 << 2;
58      static final int PRESENT_KVP = 1 << 3;
59      static final int PRESENT_MDC = 1 << 4;
60      static final int PRESENT_THROWABLE = 1 << 5;
61      static final int PRESENT_CALLER_DATA = 1 << 6;
62  
63      /** Bits of {@code Instant.getNano()} in the packed epoch field. {@code 2^30 > 10^9-1}. */
64      static final int NANO_BITS = 30;
65      static final int NANO_MASK = 0x3FFFFFFF;
66  
67      protected final FieldByteAccumulator fieldByteAccumulator = new FieldByteAccumulator();
68  
69      /**
70       * Shared by message, string arguments, KVP string values and MDC values.
71       */
72      protected final MultiEventCompressedStringSerializer longUniqueStrings = new MultiEventCompressedStringSerializer(1024);
73  
74      protected final LoggerContextVOSerializer loggerContextVOSerializer = new LoggerContextVOSerializer(1024);
75      /** Logger names are cached whole; no package-prefix table. */
76      protected final SerializerBase<String> classNameSerializer = new BasicStringSerializer(BiMapType.DIRECT_MAP, 2048);
77      protected final Serializer<String> threadNameSerializer = new BasicStringSerializer(1024);
78      protected final SerializerBase<String> messageSerializer = longUniqueStrings;
79      protected final MDCPropertyMapSerializer mdcPropertyMapSerializer = new MDCPropertyMapSerializerV5(1024, longUniqueStrings);
80      /** Frame class names use their own {@link BasicStringSerializer}. */
81      protected final Serializer<ThrowableProxyVO> throwableProxyVOSerializer = new ThrowableProxyVOSerializerV5(1024,
82              new StackTraceElementProxySerializerV5(1024, new BasicStringSerializer(BiMapType.DIRECT_MAP, 1024)));
83      protected final LongDeltaSerializer timestampDeltaSerializer = new LongDeltaSerializer();
84      protected final LongDeltaSerializer sequenceNumberDeltaSerializer = new LongDeltaSerializer();
85      protected final ArgumentArraySerializerV5 argumentArraySerializerV5 = new ArgumentArraySerializerV5(longUniqueStrings);
86      protected final CollectionSerializer<ArrayList<KeyValuePair>> kvpCollectionSerializer = new KeyValuePairCollectionSerializerV5(
87              new KeyValuePairSerializerV5(new BasicStringSerializer(1024), longUniqueStrings));
88      /** Caller-data frames. Class names have their own {@link BasicStringSerializer}. */
89      protected final Serializer<StackTraceElement> stackTraceElementSerializer =
90              new StackTraceElementSerializer(1024, new BasicStringSerializer(BiMapType.DIRECT_MAP, 1024));
91  
92      @Override
93      public void write(Kryo kryo, Output output, LoggingEventKryoVO event) {
94          LoggerContextVO loggerContextVO = event.getLoggerContextVO();
95          kryo.writeObject(output, loggerContextVO, loggerContextVOSerializer);
96          kryo.writeObject(output, event.getLoggerName(), classNameSerializer);
97          output.writeByte(LevelSerializer.levelIntToByte(event.levelInt));
98          kryo.writeObject(output, event.getThreadName(), threadNameSerializer);
99  
100         String message = event.getMessage();
101         Object[] argumentArray = event.getArgumentArray();
102         List<Marker> markerList = event.getMarkerList();
103         List<KeyValuePair> keyValuePairs = event.getKeyValuePairs();
104         Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();
105         ThrowableProxyVO throwableProxy = (ThrowableProxyVO) event.getThrowableProxy();
106         StackTraceElement[] callerData = event.hasCallerData() ? event.getCallerData() : null;
107 
108         int presence = 0;
109         if (message != null) {
110             presence |= PRESENT_MESSAGE;
111         }
112         if (argumentArray != null) {
113             presence |= PRESENT_ARGUMENTS;
114         }
115         if (markerList != null) {
116             presence |= PRESENT_MARKERS;
117         }
118         if (keyValuePairs != null) {
119             presence |= PRESENT_KVP;
120         }
121         if (mdcPropertyMap != null) {
122             presence |= PRESENT_MDC;
123         }
124         if (throwableProxy != null) {
125             presence |= PRESENT_THROWABLE;
126         }
127         if (callerData != null) {
128             presence |= PRESENT_CALLER_DATA;
129         }
130         output.writeByte(presence);
131 
132         if ((presence & PRESENT_MESSAGE) != 0) {
133             kryo.writeObject(output, message, messageSerializer);
134         }
135         if ((presence & PRESENT_ARGUMENTS) != 0) {
136             kryo.writeObject(output, argumentArray, argumentArraySerializerV5);
137         }
138         if ((presence & PRESENT_MARKERS) != 0) {
139             kryo.writeObject(output, markerList, kryo.getSerializer(ArrayList.class));
140         }
141         if ((presence & PRESENT_KVP) != 0) {
142             kryo.writeObject(output, keyValuePairs, kvpCollectionSerializer);
143         }
144         if ((presence & PRESENT_MDC) != 0) {
145             kryo.writeObject(output, mdcPropertyMap, mdcPropertyMapSerializer);
146         }
147 
148         timestampDeltaSerializer.writeLong(output, packEpoch(event.getTimeStamp(), event.getNanoseconds()));
149         sequenceNumberDeltaSerializer.writeLong(output, event.getSequenceNumber());
150 
151         if ((presence & PRESENT_THROWABLE) != 0) {
152             kryo.writeObject(output, throwableProxy, throwableProxyVOSerializer);
153         }
154         if ((presence & PRESENT_CALLER_DATA) != 0) {
155             writeCallerData(kryo, output, callerData);
156         }
157     }
158 
159     @Override
160     public LoggingEventKryoVO read(Kryo kryo, Input input, Class<? extends LoggingEventKryoVO> type) {
161         LoggerContextVO loggerContextVO = fieldByteAccumulator.read(input, "loggerContext",
162                 () -> kryo.readObject(input, LoggerContextVO.class, loggerContextVOSerializer));
163         String loggerName = fieldByteAccumulator.read(input, "loggerName",
164                 () -> kryo.readObject(input, String.class, classNameSerializer));
165         int levelInt = fieldByteAccumulator.read(input, "level",
166                 () -> LevelSerializer.byteToLevelInt(input.readByte()));
167         String threadName = fieldByteAccumulator.read(input, "threadName",
168                 () -> kryo.readObject(input, String.class, threadNameSerializer));
169 
170         int presence = fieldByteAccumulator.read(input, "presence",
171                 () -> input.readByte() & 0xFF);
172 
173         String message = ((presence & PRESENT_MESSAGE) != 0)
174                 ? fieldByteAccumulator.read(input, "message",
175                 () -> kryo.readObject(input, String.class, messageSerializer))
176                 : null;
177         Object[] argArray = ((presence & PRESENT_ARGUMENTS) != 0)
178                 ? fieldByteAccumulator.read(input, "argumentArray",
179                 () -> kryo.readObject(input, Object[].class, argumentArraySerializerV5))
180                 : null;
181         List<Marker> markerList = ((presence & PRESENT_MARKERS) != 0)
182                 ? fieldByteAccumulator.read(input, "markers",
183                 () -> kryo.readObject(input, ArrayList.class))
184                 : null;
185         List<KeyValuePair> keyValuePairs = ((presence & PRESENT_KVP) != 0)
186                 ? fieldByteAccumulator.read(input, "keyValuePairs",
187                 () -> kryo.readObject(input, ArrayList.class, kvpCollectionSerializer))
188                 : null;
189         Map<String, String> mdcPropertyMap = ((presence & PRESENT_MDC) != 0)
190                 ? fieldByteAccumulator.read(input, "mdc",
191                 () -> kryo.readObject(input, Map.class, mdcPropertyMapSerializer))
192                 : null;
193 
194         long packed = fieldByteAccumulator.readLong(input, "timestamp",
195                 () -> timestampDeltaSerializer.readLong(input));
196         long timestamp = unpackTimestamp(packed);
197         int nanoseconds = unpackNanoseconds(packed);
198         long sequenceNumber = fieldByteAccumulator.readLong(input, "sequenceNumber",
199                 () -> sequenceNumberDeltaSerializer.readLong(input));
200         ThrowableProxyVO throwableProxy = ((presence & PRESENT_THROWABLE) != 0)
201                 ? fieldByteAccumulator.read(input, "throwable",
202                 () -> kryo.readObject(input, ThrowableProxyVO.class, throwableProxyVOSerializer))
203                 : null;
204         StackTraceElement[] callerData = ((presence & PRESENT_CALLER_DATA) != 0)
205                 ? fieldByteAccumulator.read(input, "callerData",
206                 () -> readCallerData(kryo, input))
207                 : null;
208 
209         LoggingEventKryoVO.Builder eventBuilder = new LoggingEventKryoVO.Builder();
210         eventBuilder.withLoggerContextVO(loggerContextVO);
211         eventBuilder.withLoggerName(loggerName);
212         eventBuilder.withLevelInt(levelInt);
213         eventBuilder.withThreadName(threadName);
214         eventBuilder.withMessage(message);
215         eventBuilder.withArgumentArray(argArray);
216         eventBuilder.withMarkerList(markerList);
217         eventBuilder.withKeyValuePairList(keyValuePairs);
218         eventBuilder.withMdcPropertyMap(mdcPropertyMap);
219         eventBuilder.withTimestamp(timestamp);
220         eventBuilder.withNanoseconds(nanoseconds);
221         eventBuilder.withSequenceNumber(sequenceNumber);
222         eventBuilder.withThrowableProxy(throwableProxy);
223         eventBuilder.withCallerData(callerData);
224         return eventBuilder.build();
225     }
226 
227     public void writeCallerData(Kryo kryo, Output output, StackTraceElement[] callerData) {
228         output.writeVarInt(callerData.length, true);
229         for (StackTraceElement ste : callerData) {
230             kryo.writeObject(output, ste, stackTraceElementSerializer);
231         }
232     }
233 
234     public StackTraceElement[] readCallerData(Kryo kryo, Input input) {
235         int length = input.readVarInt(true);
236         StackTraceElement[] callerData = new StackTraceElement[length];
237         for (int i = 0; i < length; i++) {
238             callerData[i] = kryo.readObject(input, StackTraceElement.class, stackTraceElementSerializer);
239         }
240         return callerData;
241     }
242 
243     /**
244      * Packs epoch-millisecond timestamp and nano-of-second. {@code timestamp / 1000}
245      * is {@link java.time.Instant#getEpochSecond()} for non-negative timestamps
246      * (these streams are post-1970).
247      */
248     static long packEpoch(long timestamp, int nanoseconds) {
249         long epochSecond = timestamp / 1000;
250         return (epochSecond << NANO_BITS) | (nanoseconds & NANO_MASK);
251     }
252 
253     static long unpackTimestamp(long packed) {
254         long epochSecond = packed >>> NANO_BITS;
255         int nanoseconds = unpackNanoseconds(packed);
256         return epochSecond * 1000 + nanoseconds / 1_000_000;
257     }
258 
259     static int unpackNanoseconds(long packed) {
260         return (int) (packed & NANO_MASK);
261     }
262 
263     @Override
264     public FieldByteAccumulator getFieldByteAccumulator() {
265         return fieldByteAccumulator;
266     }
267 }