Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-2670

Add an option to customize the ObjectMapper for JSONLayout

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.11.2, 2.12.0
    • None
    • Layouts
    • None
    • Java 11, Spring Boot 2.1.5 with Log4J 2.11.2.

    Description

      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 IllegalArgumentExceptions 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 Mixins 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.

      As discussed on the Mailinglist, this might be solved in several different ways, e.g. with the Plugin mechanism or with an explicit modifyObjectMapper hook.

      Attachments

        Activity

          People

            Unassigned Unassigned
            dadadom Dominik Sandjaja
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: