Dominik Sandjaja created LOG4J2-2670:
----------------------------------------

             Summary: Add an option to customize the ObjectMapper for JSONLayout
                 Key: LOG4J2-2670
                 URL: https://issues.apache.org/jira/browse/LOG4J2-2670
             Project: Log4j 2
          Issue Type: Improvement
          Components: Layouts
    Affects Versions: 2.12.0, 2.11.2
         Environment: Java 11, Spring Boot 2.1.5 with Log4J 2.11.2.
            Reporter: Dominik Sandjaja


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
  
{code:java}
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)
{code}
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.

As [discussed on the 
Mailinglist|[https://lists.apache.org/thread.html/4bd9ddec4786f602b50a8e2bc9f26e990c2ffba8c433ff5ab5d54f1a@%3Clog4j-user.logging.apache.org%3E]],
 this might be solved in several different ways, e.g. with the {{Plugin}} 
mechanism or with an explicit {{modifyObjectMapper}} hook.



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)

Reply via email to