[ https://issues.apache.org/jira/browse/LOG4J2-2641?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16871779#comment-16871779 ]
Ralph Goers edited comment on LOG4J2-2641 at 6/24/19 9:08 PM: -------------------------------------------------------------- Well, there are a few ways you could handle this: 1. Use a MapMessage and place the payload in the map with a key of "request" and the message string with a key of "message". I haven't tried this but looking it the code it seems like you could then specify a pattern layout of "%msg\{JSON}" to get the Map formatted as JSON. The code for this would look like: {code:java} logger.info(new MapMesssage().putAll(Map.of("request", payload, "message", "here is my application message'));{code} Note that Map.of requires Java 9. 2. Create a custom Message that implements getFormatedMessage to return JSON. With this you could do {code:java} logger.info(new CustomMapMessage("here is my application message", payload));{code} getFormattedMessage would then render the JSON however it wants. 3. Create a custom Message factory that creates a MapMessage by taking the message and placing it in the message key and parameters and placing them in their own keys. So the developer code just code {code:java} logger.info("here is my application message", payload);{code} This is essentially the same as the previous example but makes life simpler for the developer. You would just have to configure the MessageFactory to be used. 4. Use a custom layout. Inspect the incoming Message and don't call getFormattedMessage. Call getFormat() the get the raw message string and call getParameters to get the extra fields. Convert that to json. Unfortunately, the PatternLayout won't really work here as the is no pattern converter to access the parameters. That would certainly be a valid Jira issue. was (Author: ralph.go...@dslextreme.com): Well, there are a few ways you could handle this: # Use a MapMessage and place the payload in the map with a key of "request" and the message string with a key of "message". I haven't tried this but looking it the code it seems like you could then specify a pattern layout of "%msg\{JSON}" to get the Map formatted as JSON. The code for this would look like: {code:java} logger.info(new MapMesssage().putAll(Map.of("request", payload, "message", "here is my application message'));{code} Note that Map.of requires Java 9. # Create a custom Message that implements getFormatedMessage to return JSON. With this you could do {code:java} logger.info(new CustomMapMessage("here is my application message", payload));{code} getFormattedMessage would then render the JSON however it wants. # Create a custom Message factory that creates a MapMessage by taking the message and placing it in the message key and parameters and placing them in their own keys. So the developer code just code {code:java} logger.info("here is my application message", payload);{code} This is essentially the same as the previous example but makes life simpler for the developer. You would just have to configure the MessageFactory to be used. # Use a custom layout. Inspect the incoming Message and don't call getFormattedMessage. Call getFormat() the get the raw message string and call getParameters to get the extra fields. Convert that to json. Unfortunately, the PatternLayout won't really work here as the is no pattern converter to access the parameters. That would certainly be a valid Jira issue. > How to Log object entries outside the message entry > --------------------------------------------------- > > Key: LOG4J2-2641 > URL: https://issues.apache.org/jira/browse/LOG4J2-2641 > Project: Log4j 2 > Issue Type: Question > Reporter: Peter Podniesinski > Priority: Trivial > > Hi Team. I could use some advice how to craft a log statement that prints > both a String Message along with a request json details. > If I do something like this it gets stringified and shows up in the > `_message_` field in Logstash. > {code:java} > logger.info("Create Account Application. Request: {}", > gson.toJson(depositApplicationRequest));`{code} > > > Ideally I want this to show up looking like: > > {code:java} > { request: ..., > message: 'Create Account Application' }{code} > > > -- This message was sent by Atlassian JIRA (v7.6.3#76005)