GitHub user mdvorak opened a pull request:

    https://github.com/apache/logging-log4j2/pull/110

    JsonLayout support for custom key-value pairs being inserted into JSON

    _Note: This is not ready PR, there are no unit tests for new feature - 
please review before i'll invest more time into it._
    
    * Changed `AbstractJacksonLayout.convertMutableToLog4jEvent` to `protected 
Object wrapLogEvent(LogEvent event)`
    * Added Extras as `KeyValuePair[]` to `JsonLayout`
    * When extras as specified, `LogEvent` is wrapped in new 
`LogEventWithExtras` class, which merges `LogEvent` serialized data with 
provided extras
    
    This is generally wanted feature, for example see here:
    https://github.com/ggrandes/log4j2-simplejson
    https://github.com/majikthys/log4j2-logstash-jsonevent-layout
    
    How it works:
    ```xml
    <JsonLayout>
      <KeyValuePair key="hostName" value="${hostName}"/>
    </JsonLayout>
    ```
    Inserts into each generated JSON `hostName` key:
    ```json
    {
    "timeMillis":1505228896690,
    "thread":"main",
    "level":"INFO",
    "loggerName":"org.springframework....",
    "message":"Refreshing ...",
    "endOfBatch":false,
    "loggerFqcn":"org....",
    "contextMap":{},
    "threadId":1,
    "threadPriority":5,
    "hostName":"wp21257b"
    }
    ```
    
    This is accomplished by tricks with Jackon json serializer:
    ```java
    public static class LogEventWithExtras {
        // ....
        @JsonUnwrapped // Deserializes actual LogEvent into root object
        public Object getLogEvent() { return logEvent; }
    
        @JsonAnyGetter // Adds everything from the map into root object
        public Map<String, Object> getExtras() { return extras; }
    }
    ```

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/mdvorak/logging-log4j2 jsonlayout-extras29

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/logging-log4j2/pull/110.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #110
    
----
commit ba78e4b08727d2ce558120731d40133122a9fb05
Author: Michal Dvorak (cen38289) <michadvo...@csas.cz>
Date:   2017-09-12T12:31:52Z

    JsonLayout support for custom key-value pairs being inserted into JSON
    
    Changed AbstractJacksonLayout.convertMutableToLog4jEvent to protected 
Object wrapLogEvent(LogEvent event)
    Added Extras as KeyValuePair[] to JsonLayout
    When extras as specified, LogEvent is wrapped in new LogEventWithExtras 
class, which merges LogEvent serialized data with provided extras

----


---

Reply via email to