Hello Dominik,

I don't know how you can achieve that using JsonLayout. That said, it
sounds like a really valid use case and I have already created a
log4j2-logstash-layout ticket for it
<https://github.com/vy/log4j2-logstash-layout/issues/35>. I plan to
implement it in a couple of weeks and cut a new release. I will post a
follow-up to this thread when that happens.

Cheers.

On Fri, Jul 12, 2019 at 2:38 PM 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
>

Reply via email to