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

Volkan Yazici commented on LOG4J2-3138:
---------------------------------------

LOG4J2-3082 addresses this to a certain extent. There {{JsonTemplateLayout}} 
will allow falling back to external JSON serializers, e.g., Jackson. Note that 
this is only relevant while serializing messages and MDC, this doesn't enable 
you to customize the JSON structure {{LogEvent}} is rendered to. In its current 
form, {{LogEvent}} JSON structure is described by the JSON template. Letting 
people to pass this structure by annotations on a POJO is indeed an interesting 
idea, yet I am not inclined to go this route due to following concerns:
* We will need to duplicate every single JTL functionality along with their 
configuration logic in annotations. This translates to at least 2-3 dozens of 
annotations. I would rather see a real world use case before pushing such a 
change to the code base.
* Almost every Log4j configuration lives in a separate file – this is aligned 
with the idea of providing the JSON template in a separate file.
* One might argue that they need to provide the JSON template programmatically. 
That is still perfectly possible:
{code:java}
// writeJson(Object) can be implemented either using Jackson or JTL's 
JsonWriter.
String eventTemplate = writeJson(Map.of(
        "ex_stacktrace", Map.of(
                "$resolver", "exception",
                "field", "stackTrace",
                "stringified", true)));

JsonTemplateLayout layout = JsonTemplateLayout
        .newBuilder()
        .setConfiguration(configuration)
        .setStackTraceEnabled(true)
        .setEventTemplate(eventTemplate)
        .build();
{code}
* I personally think Log4j suffers from a feature creep when it comes to JSON: 
3 competing JSON layouts ({{GelfLayout}}, {{JsonLayout}}, 
{{JsonTemplateLayout}}), many independent JSON utilities 
({{StringBuilders.escapeJson()}}, {{MapMessageJsonFormatter}}, JTL's 
{{Json\{Reader,Writer\}}}), etc. I wanted to remove all this bloat in 3.x and 
settle with JTL, but rejected due to backward compatibility concerns. I think 
the proposed {{EnhancedJsonLayout}} will only worsen the situation.

> Define a custom model for a json layout for creating custom json logs output
> ----------------------------------------------------------------------------
>
>                 Key: LOG4J2-3138
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-3138
>             Project: Log4j 2
>          Issue Type: New Feature
>          Components: Core
>            Reporter: Alireza Vahedi
>            Assignee: Volkan Yazici
>            Priority: Major
>   Original Estimate: 24h
>  Remaining Estimate: 24h
>
> In current json layout user can not define a custom json structure for json 
> logs. For such purpose they have to define new layout. Why new layout is 
> required when just the model structure of json has been changed?
> I think we need a enhanced json layout that gets some class as target 
> structure for generating json of logs, and user can configure it in log4j2 
> xml configurable file.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to