You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Tatu Saloranta (Jira)" <ji...@apache.org> on 2019/10/07 15:28:00 UTC

[jira] [Commented] (LOG4J2-2670) Add an option to customize the ObjectMapper for JSONLayout

    [ https://issues.apache.org/jira/browse/LOG4J2-2670?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16945962#comment-16945962 ] 

Tatu Saloranta commented on LOG4J2-2670:
----------------------------------------

One technical aspect that might be of interest is that aside from using mix-in (and add `@JsonIgnoreType` on `TypeDescriptor`, or `@JsonIgnore` on whatever includes it), it is also possible to add "config override" on `ObjectMapper`:
{noformat}
mapper.configOverride(TypeDescriptor.class).setIsIgnoredType(true);{noformat}
This does not remove need to configure mapper instances but can be more convenient.

Related to configuration: once dependency to Jackson is upgraded to 2.10, there is also a way to use Builder style construction for mappers, something like:
{noformat}
ObjectMapper mapper = JsonMapper.builder()
    .disable(MapperFeature.USE_ANNOTATIONS)
    .build();{noformat}
and the benefit here could be that exposing Builder for configuration could be bit safer than exposing full mapper object.

I don't know if either of above helps with bigger question of how to allow configurability but thought it might be helpful.

> 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.11.2, 2.12.0
>         Environment: Java 11, Spring Boot 2.1.5 with Log4J 2.11.2.
>            Reporter: Dominik Sandjaja
>            Priority: Major
>
> 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
>   
>  {code:java}
> com.fasterxml.jackson.databind.ObjectMapper().writeValueAsString(org.springframework.core.convert.TypeDescriptor.valueOf(String.javaClass))
> {code}
>   
> 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 {{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|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
(v8.3.4#803005)