Hi Dominik I see two ways of addressing this: 1) Add a setObjectMapper(ObjectMapper) to org.apache.logging.log4j.core.layout.JsonLayout.Builder This would work well for programmatic configurations and/or: 2) Add a setObjectMapperFactory(String) to org.apache.logging.log4j.core.layout.JsonLayout.Builder This would add a class name for implementors of a new interface ObjectMapperFactory. This would work nicely for configuration files.
To keep all users in play, 2) seems best. Thoughts? Gary On Fri, Jul 12, 2019 at 8:38 AM Dominik Sandjaja < dominik.sandj...@trivago.com> wrote: > Hello, > > we are using the JSON layout in our Spring Boot application to easily > ingest the logs in Elasticsearch. > > One message that we log is about failed conversions, when an incoming > request parameter cannot be converted correctly. As we use Spring’s > Converter functionality for this, these `IllegalArgumentException`s are > wrapped in a `ConversionFailedException`. This exception has fields of type > `TypeDescriptor` which hold information about the source and the target > type of the conversion. > > The problem is, that these `TypeDescriptor` objects cannot be serialized > to JSON. The simple call > > > com.fasterxml.jackson.databind.ObjectMapper().writeValueAsString(org.springframework.core.convert.TypeDescriptor.valueOf(String.javaClass)) > > throws an exception like > > ERROR StatusLogger > com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Direct > self-reference leading to cycle (through reference chain: > org.springframework.core.ResolvableType["componentType"]->org.springframework.core.ResolvableType["componentType"]) > com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Direct > self-reference leading to cycle (through reference chain: > org.springframework.core.ResolvableType["componentType"]->org.springframework.core.ResolvableType["componentType"]) > at > com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) > at > com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191) > at > com.fasterxml.jackson.databind.ser.BeanPropertyWriter._handleSelfReference(BeanPropertyWriter.java:944) > at > com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:721) > at > com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) > at > com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) > > The error can be prevented if `Mixin`s are added to the `ObjectMapper` > which ignore the corresponding class. > > The `Log4jJsonObjectMapper` is initialized in the `JacksonFactory.JSON` > class and I currently do not see any way to somehow configure this > ObjectMapper with additional MixIns. > > Hence, my question finally is: > How can I configure Log4J’s JSON mapping to not break on such > non-serializable log content? > > Thank you! > Dominik >