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

Remko Popma commented on LOG4J2-623:
------------------------------------

*Quick summary of the problem*

XmlLayout, JsonLayout and YamlLayout have a shared code base but for the thread 
context we want to output XML in a different format from JSON/YAML.

Current JSON format:
{code}
  "contextMap" : [ {
    "key" : "foo",
    "value" : "FOO"
  }, {
    "key" : "bar",
    "value" : "BAR"
  } ]
{code}

This is OK for XML (and has the advantage the document structure can be 
captures in a schema) but is not desirable for JSON & YAML where the natural 
mapping is something like:
{code}
"contextMap": {
 "foo": "FOO",
 "bar": "BAR"
}
{code}


*Quick analysis of how this could be implemented*
 (I'm new to Jackson, so below is just guess work)

* context map output is created by {{ListOfMapEntrySerializer}}. This class is 
currently used for all Jackson-generated layouts (XML, JSON, YAML). To keep XML 
the same but change JSON and YAML we need to create a different serializer 
class for JSON, YAML.
* ListOfMapEntrySerializer creates an array of MapEntry objects to output 
{{\[\{"key" : "xxxxx", "value" : "yyyyy"\}\]}}. Instead, the new serializer 
should probably simply call something like the below because the key is not 
fixed:

{code}
JsonGenerator jgen;
jgen.writeStartObject();
for (Entry<String, String> entry : map.entrySet()) {
    jgen.writeStringField(entry.getKey(), entry.getValue());
}
jgen.writeEndObject();
{code}

* {{LogEventMixIn#getContextMap}} has the annotation that determines which 
serializer to use. Looks like we need to separate this into XmlLogEventMixIn 
and JsonYamlLogEventMixIn so we can annotate the {{getContextMap}} method with 
the new serializer.
* The mix-in is installed by {{Initializers.SetupContextInitializer}}, which is 
invoked by all Log4jXmlModule, Log4jYamlModule and Log4jJsonModule: we may need 
to split this for the separate layouts

> Better structure of Thread Context Map in JSONLayout
> ----------------------------------------------------
>
>                 Key: LOG4J2-623
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-623
>             Project: Log4j 2
>          Issue Type: Improvement
>          Components: Layouts
>    Affects Versions: 2.0-rc1, 2.0, 2.0.1
>            Reporter: Mikael Ståldal
>            Assignee: Ralph Goers
>            Priority: Minor
>             Fix For: 2.0-rc2
>
>
> Currently, the Thread Context Map looks like this in JSONLayout:
> {code:JavaScript|title=Current}
>  "Properties":[
>       {
>         "name":"UserName",
>         "value":"admin"
>       },
>       {
>         "name":"OrgName",
>         "value":"test"
>       } 
>     ] 
> {code}
> This does not properly make use of the JSON data format. Since the Thread 
> Context Map is a map, it should be represented as a JSON object. And why not 
> name it "mdc" rather than the quite vauge "Properties"?
> {code:JavaScript|title=Suggested}
>   "mdc": {
>     "UserName":"admin", 
>     "OrgName":"test"
>   }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to