logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dominik Sandjaja <Dominik.Sandj...@trivago.com>
Subject How to configure ObjectMapper of Log4j2 JSON Layout
Date Fri, 12 Jul 2019 12:38:17 GMT
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
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message