diff --git a/core/src/main/kotlin/net/corda/core/serialization/DefaultKryoCustomizer.kt b/core/src/main/kotlin/net/corda/core/serialization/DefaultKryoCustomizer.kt index d961cbe195..42d9f4719c 100644 --- a/core/src/main/kotlin/net/corda/core/serialization/DefaultKryoCustomizer.kt +++ b/core/src/main/kotlin/net/corda/core/serialization/DefaultKryoCustomizer.kt @@ -17,6 +17,7 @@ import net.corda.core.utilities.NonEmptySetSerializer import net.i2p.crypto.eddsa.EdDSAPrivateKey import net.i2p.crypto.eddsa.EdDSAPublicKey import org.objenesis.strategy.StdInstantiatorStrategy +import org.slf4j.Logger import java.io.BufferedInputStream import java.util.* @@ -34,7 +35,7 @@ object DefaultKryoCustomizer { // for change to a class. setDefaultSerializer(CompatibleFieldSerializer::class.java) // Take the safest route here and allow subclasses to have fields named the same as super classes. - fieldSerializerConfig.setCachedFieldNameStrategy(FieldSerializer.CachedFieldNameStrategy.EXTENDED) + fieldSerializerConfig.cachedFieldNameStrategy = FieldSerializer.CachedFieldNameStrategy.EXTENDED // Allow construction of objects using a JVM backdoor that skips invoking the constructors, if there is no // no-arg constructor available. @@ -78,8 +79,10 @@ object DefaultKryoCustomizer { register(MetaData::class.java, MetaDataSerializer) register(BitSet::class.java, ReferencesAwareJavaSerializer) + addDefaultSerializer(Logger::class.java, LoggerSerializer) + val customization = KryoSerializationCustomization(this) pluginRegistries.forEach { it.customizeSerialization(customization) } } } -} \ No newline at end of file +} diff --git a/core/src/main/kotlin/net/corda/core/serialization/Kryo.kt b/core/src/main/kotlin/net/corda/core/serialization/Kryo.kt index a93e5e5140..c99356d3fa 100644 --- a/core/src/main/kotlin/net/corda/core/serialization/Kryo.kt +++ b/core/src/main/kotlin/net/corda/core/serialization/Kryo.kt @@ -5,7 +5,6 @@ import com.esotericsoftware.kryo.io.Input import com.esotericsoftware.kryo.io.Output import com.esotericsoftware.kryo.pool.KryoPool import com.esotericsoftware.kryo.serializers.JavaSerializer -import com.esotericsoftware.kryo.serializers.MapSerializer import com.esotericsoftware.kryo.util.MapReferenceResolver import com.google.common.annotations.VisibleForTesting import net.corda.core.contracts.* @@ -17,6 +16,8 @@ import net.i2p.crypto.eddsa.EdDSAPrivateKey import net.i2p.crypto.eddsa.EdDSAPublicKey import net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec +import org.slf4j.Logger +import org.slf4j.LoggerFactory import java.io.* import java.lang.reflect.InvocationTargetException import java.nio.file.Files @@ -579,3 +580,15 @@ object MetaDataSerializer : Serializer() { return MetaData(schemeCodeName, versionID, signatureType, timestamp, visibleInputs, signedInputs, merkleRoot, publicKey) } } + +/** For serialising a Logger. */ +@ThreadSafe +object LoggerSerializer : Serializer() { + override fun write(kryo: Kryo, output: Output, obj: Logger) { + output.writeString(obj.name) + } + + override fun read(kryo: Kryo, input: Input, type: Class): Logger { + return LoggerFactory.getLogger(input.readString()) + } +} diff --git a/core/src/test/kotlin/net/corda/core/serialization/KryoTests.kt b/core/src/test/kotlin/net/corda/core/serialization/KryoTests.kt index 4b25645434..992020f94e 100644 --- a/core/src/test/kotlin/net/corda/core/serialization/KryoTests.kt +++ b/core/src/test/kotlin/net/corda/core/serialization/KryoTests.kt @@ -11,11 +11,13 @@ import org.bouncycastle.pqc.jcajce.provider.BouncyCastlePQCProvider import org.junit.After import org.junit.Before import org.junit.Test +import org.slf4j.LoggerFactory import java.io.InputStream import java.security.Security import java.time.Instant import java.util.* import kotlin.test.assertEquals +import kotlin.test.assertTrue class KryoTests { @@ -125,6 +127,14 @@ class KryoTests { assertEquals(meta2, meta) } + @Test + fun `serialize - deserialize Logger`() { + val logger = LoggerFactory.getLogger("aName") + val logger2 = logger.serialize(storageKryo()).deserialize(storageKryo()) + assertEquals(logger.name, logger2.name) + assertTrue(logger === logger2) + } + @CordaSerializable private data class Person(val name: String, val birthday: Instant?)