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.StackTraceElementProxy;
17  import ch.qos.logback.core.kryo.SerializerBase;
18  import com.esotericsoftware.kryo.kryo5.Kryo;
19  import com.esotericsoftware.kryo.kryo5.io.Input;
20  import com.esotericsoftware.kryo.kryo5.io.Output;
21  import com.esotericsoftware.kryo.kryo5.minlog.Log;
22  
23  /**
24   * Caching serializer for {@link StackTraceElementProxy}.
25   *
26   * <p>Stack frames are the main source of redundancy in throwable payloads: the same frames
27   * reappear across exceptions and log events. This serializer:
28   * <ul>
29   *   <li>Caches whole frames via {@link BiMapType#DIRECT_MAP} (frame equality is well-defined)</li>
30   *   <li>Compresses class names with {@link ClassNameSerializer} by default (V5 passes a
31   *       {@link BasicStringSerializer})</li>
32   *   <li>Caches method and file names with {@link SingleItemCompressingStringSerializer}</li>
33   * </ul>
34   */
35  public class StackTraceElementProxySerializerV5 extends SerializerBase<StackTraceElementProxy> {
36  
37      private static final int DEFAULT_CAPACITY = 4096;
38  
39      private final SerializerBase<String> classNameSerializer;
40      private final SingleItemCompressingStringSerializer methodNameSerializer;
41      private final SingleItemCompressingStringSerializer fileNameSerializer;
42  
43      public StackTraceElementProxySerializerV5() {
44          this(DEFAULT_CAPACITY);
45      }
46  
47      /**
48       * Class names use {@link ClassNameSerializer}.
49       */
50      public StackTraceElementProxySerializerV5(int capacity) {
51          this(capacity, new ClassNameSerializer(capacity));
52      }
53  
54      /**
55       * @param classNameSerializer class-name codec; V5 passes a {@link BasicStringSerializer}
56       */
57      public StackTraceElementProxySerializerV5(int capacity, SerializerBase<String> classNameSerializer) {
58          super(BiMapType.DIRECT_MAP, capacity);
59          setAcceptsNull(false);
60          this.classNameSerializer = classNameSerializer;
61          this.methodNameSerializer = new SingleItemCompressingStringSerializer(capacity);
62          // acceptsNull stays false: Kryo writeObjectOrNull handles null file names via
63          // NULL/NOT_NULL flags. StringSerializer rejects null values.
64          this.fileNameSerializer = new SingleItemCompressingStringSerializer(capacity);
65      }
66  
67      @Override
68      protected void writeNewEntry(Kryo kryo, Output output, StackTraceElementProxy step) {
69          StackTraceElement ste = step.getStackTraceElement();
70          Log.trace("writeNewEntry for StackTraceElementProxy: " + ste);
71  
72          // Rarely set; store raw (null-friendly) strings
73          output.writeString(ste.getClassLoaderName());
74          output.writeString(ste.getModuleName());
75          output.writeString(ste.getModuleVersion());
76  
77          // High-redundancy string fields
78          kryo.writeObject(output, ste.getClassName(), classNameSerializer);
79          kryo.writeObject(output, ste.getMethodName(), methodNameSerializer);
80          kryo.writeObjectOrNull(output, ste.getFileName(), fileNameSerializer);
81  
82          // Line numbers can be negative (-1 unknown, -2 native)
83          output.writeVarInt(ste.getLineNumber(), false);
84      }
85  
86      @Override
87      protected StackTraceElementProxy readNewEntry(Kryo kryo, Input input) {
88          String classLoaderName = input.readString();
89          String moduleName = input.readString();
90          String moduleVersion = input.readString();
91          String className = kryo.readObject(input, String.class, classNameSerializer);
92          String methodName = kryo.readObject(input, String.class, methodNameSerializer);
93          String fileName = kryo.readObjectOrNull(input, String.class, fileNameSerializer);
94          int lineNumber = input.readVarInt(false);
95  
96          StackTraceElement ste = new StackTraceElement(classLoaderName, moduleName, moduleVersion,
97                  className, methodName, fileName, lineNumber);
98          Log.trace("readNewEntry for StackTraceElementProxy: " + ste);
99          return new StackTraceElementProxy(ste);
100     }
101 }