001/* 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v2.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.classic.kryo; 015 016import ch.qos.logback.classic.spi.LoggerContextVO; 017import com.esotericsoftware.kryo.kryo5.Kryo; 018import com.esotericsoftware.kryo.kryo5.Serializer; 019import com.esotericsoftware.kryo.kryo5.io.Input; 020import com.esotericsoftware.kryo.kryo5.serializers.MapSerializer; 021import com.esotericsoftware.kryo.kryo5.util.MapReferenceResolver; 022import org.slf4j.event.KeyValuePair; 023 024import java.util.Collections; 025import java.util.HashMap; 026import java.util.Map; 027 028 029public class KryoBuilder { 030 031 032 static public Kryo build(EncoderVersionTag encoderVersionTag) { 033 return build(encoderVersionTag.serializerType(), encoderVersionTag.streamVersion()); 034 } 035 036 /** 037 * Build a Kryo instance for the given serializer type and stream version. 038 */ 039 static public Kryo build(KryoEncoder.SerializerType serializerType, byte streamVersion) { 040 switch (serializerType) { 041 case Vanilla: 042 return buildLoggerEventKryoVO_Vanilla(); 043 case Custom: 044 return buildCustomForVersion(streamVersion); 045 default: 046 throw new IllegalArgumentException("Unknown serializer type: " + serializerType); 047 } 048 } 049 050 static Kryo buildCustomForVersion(byte streamVersion) { 051 switch (streamVersion) { 052 case KryoClassicConstants.STREAM_VERSION_5: 053 return buildLoggerEventKryoVO_CustomV5(); 054 default: 055 throw new IllegalArgumentException("Unsupported stream version for Custom: " 056 + (streamVersion & 0xFF)); 057 } 058 } 059 060 static public Kryo buildLoggerEventKryoVO_Vanilla() { 061 062 MapReferenceResolver mapReferenceResolver = new MapReferenceResolver(); 063 Kryo kryo = new Kryo(mapReferenceResolver); 064 065 kryo.register(LoggingEventKryoVO.class); 066 kryo.register(LoggerContextVO.class, new VanillaLoggerContextVOSerializer()); 067 //kryo.register(Object.class); 068 kryo.register(Object[].class); 069 kryo.register(HashMap.class); 070 kryo.register(java.util.ArrayList.class); 071 072 //kryo.register(org.slf4j.helpers.BasicMarker.class); 073 kryo.register(java.util.concurrent.CopyOnWriteArrayList.class); 074 kryo.register(ch.qos.logback.classic.spi.IThrowableProxy[].class); 075 kryo.register(ch.qos.logback.classic.spi.ThrowableProxyVO.class, new VanillaThrowableProxyVOSerializer()); 076 kryo.register(ch.qos.logback.classic.spi.StackTraceElementProxy[].class); 077 kryo.register(ch.qos.logback.classic.spi.StackTraceElementProxy.class, new StackTraceElementProxySerializer()); 078 kryo.register(StackTraceElement[].class); 079 kryo.register(java.lang.StackTraceElement.class, new StackTraceElementSerializer()); 080 kryo.register(org.slf4j.helpers.BasicMarker.class, new BasicMarkerSerializer()); 081 kryo.register(KeyValuePair.class, new KeyValuePairSerializer()); 082 // Appended so existing vanilla class ids stay stable. Logback's snapshot 083 // is this wrapper; FieldSerializer cannot construct it. 084 kryo.register(Collections.unmodifiableMap(new HashMap<String, String>()).getClass(), 085 new MapSerializer<Map<String, String>>() { 086 @Override 087 protected Map<String, String> create(Kryo kryo, Input input, 088 Class<? extends Map<String, String>> type, int size) { 089 return new HashMap<>(Math.max(4, (int) (size / 0.75f) + 1)); 090 } 091 }); 092 return kryo; 093 } 094 095 096 /** 097 * Custom codec for stream version 5 ({@link LoggingEventKryoVOSerializerV5}). 098 */ 099 static public Kryo buildLoggerEventKryoVO_CustomV5() { 100 return buildCustomKryo(new LoggingEventKryoVOSerializerV5()); 101 } 102 103 104 static Kryo buildCustomKryo(Serializer<LoggingEventKryoVO> eventSerializer) { 105 Kryo kryo = new Kryo(); 106 107 kryo.setReferences(false); 108 109 kryo.register(LoggingEventKryoVO.class, eventSerializer); 110 kryo.register(Object[].class); 111 112 kryo.register(StackTraceElement[].class); 113 kryo.register(java.util.ArrayList.class); 114 115 // events are written with explicit serializers, the registration only reserves the class id. 116 // Avoid the default FieldSerializer which requires reflective access to ch.qos.logback.classic.spi 117 kryo.register(ch.qos.logback.classic.spi.ThrowableProxyVO.class, new VanillaThrowableProxyVOSerializer()); 118 kryo.register(ch.qos.logback.classic.spi.IThrowableProxy[].class); 119 kryo.register(org.slf4j.helpers.BasicMarker.class, new BasicMarkerSerializer()); 120 kryo.register(KeyValuePair.class, new KeyValuePairSerializer()); 121 kryo.register(java.util.concurrent.CopyOnWriteArrayList.class); 122 kryo.register(ch.qos.logback.classic.spi.StackTraceElementProxy[].class); 123 124 kryo.register(java.util.HashMap.class); 125 126 return kryo; 127 } 128}