[ 
https://issues.apache.org/jira/browse/LOG4J2-3298?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Matt Pavlovich updated LOG4J2-3298:
-----------------------------------
    Description: 
Currently, if a user's object is able to generate well-formed JSON string, it 
will be escaped. It would be great if there was a way to config the 
JsonTemplateLayout to _not escape_ certain payloads. This allows the json to be 
fully formed and contain model object data that can be parsed out later without 
any un-formatting. 

Example log entry:
{noformat}
{
  "instant": {
    "epochSecond": 1640632561,
    "nanoOfSecond": 287515000
  },
  "thread": "main",
  "level": "INFO",
  "loggerName": "io.hyte.dev.c.ServiceC",
  "message": "Doing way other thing with order: 3235",
  "contextStack": ["{ \"Task\" {\
      "id\":\"3623\", \"orderId\": "1345\", \"taskStatus\":null, 
\"startDateTime\": \"2021-12-27T06:07:01.363232-06:00\", \"endDateTime\": 
\"2021-12-27T17:10:01.365430-06:00\"} }",
      "{ \"Order\" { \"id\": \"3235\", \"customerName\": \"Customer-8780\", 
\"createdDate\" :\"2021-12-27T08:55:01.366162-06:00\"} }"], 
      
  "endOfBatch": false, 
  "loggerFqcn": "org.apache.logging.log4j.spi.AbstractLogger", 
  "threadId": 1, 
  "threadPriority": 5
}
{noformat}

Desired output:
{noformat}
{
  "instant": {
    "epochSecond": 1640632561,
    "nanoOfSecond": 287515000
  },
  "thread": "main",
  "level": "INFO",
  "loggerName": "io.hyte.dev.c.ServiceC",
  "message": "Doing way other thing with order: 3235",
  "contextStack": [{ "Task" { "id": "3623", "orderId": "1345", 
"taskStatus":null, "startDateTime": "2021-12-27T06:07:01.363232-06:00", 
"endDateTime": "2021-12-27T17:10:01.365430-06:00"} }",
                   { "Order" { "id": "3235", "customerName": "Customer-8780", 
"createdDate": "2021-12-27T08:55:01.366162-06:00"} }],
  "endOfBatch": false,
  "loggerFqcn": "org.apache.logging.log4j.spi.AbstractLogger",
  "threadId": 1,
  "threadPriority": 5
}
{noformat}

Proposed requirements:
1. Users would have to pre-escape their JSON string in order to not break 
overall log json format

Implementation approach options:

1. Prefix {noformat}{noesc}{noformat} or other marker macro to instruct 
JSONWriter skip escaping for that string

{noformat}
ThreadContext.push("{noesc}" + JSONString(myModelObject));
{noformat}

_or_

2. Alternatively, the JSONWriter could invoke a JSONReader on the marked 
payload and then ensure that fields are properly escaped, but not escape the 
whole payload. This would involve add'l cycles, but remove the pre-req and 
potential hazard of user calling with malformed JSON

Developer codes:
{noformat}
ThreadContext.push("{json}" + JSONString(myModelObject));
...
JSONWriter detects prefix {json}.. then invokes JSONReader on the payload
{noformat}



  was:
Currently, if a JSON string is pushed to ThreadContext, it will be escaped. It 
would be great if there was a way to config the JsonTemplateLayout to _not 
escape_ certain payloads. This allows the json to be fully formed and contain 
model object data that can be parsed out later without any un-formatting. 

Example log entry:
{noformat}
{
  "instant": {
    "epochSecond": 1640632561,
    "nanoOfSecond": 287515000
  },
  "thread": "main",
  "level": "INFO",
  "loggerName": "io.hyte.dev.c.ServiceC",
  "message": "Doing way other thing with order: 3235",
  "contextStack": ["{ \"Task\" {\
      "id\":\"3623\", \"orderId\": "1345\", \"taskStatus\":null, 
\"startDateTime\": \"2021-12-27T06:07:01.363232-06:00\", \"endDateTime\": 
\"2021-12-27T17:10:01.365430-06:00\"} }",
      "{ \"Order\" { \"id\": \"3235\", \"customerName\": \"Customer-8780\", 
\"createdDate\" :\"2021-12-27T08:55:01.366162-06:00\"} }"], 
      
  "endOfBatch": false, 
  "loggerFqcn": "org.apache.logging.log4j.spi.AbstractLogger", 
  "threadId": 1, 
  "threadPriority": 5
}
{noformat}

Desired output:
{noformat}
{
  "instant": {
    "epochSecond": 1640632561,
    "nanoOfSecond": 287515000
  },
  "thread": "main",
  "level": "INFO",
  "loggerName": "io.hyte.dev.c.ServiceC",
  "message": "Doing way other thing with order: 3235",
  "contextStack": [{ "Task" { "id": "3623", "orderId": "1345", 
"taskStatus":null, "startDateTime": "2021-12-27T06:07:01.363232-06:00", 
"endDateTime": "2021-12-27T17:10:01.365430-06:00"} }",
                   { "Order" { "id": "3235", "customerName": "Customer-8780", 
"createdDate": "2021-12-27T08:55:01.366162-06:00"} }],
  "endOfBatch": false,
  "loggerFqcn": "org.apache.logging.log4j.spi.AbstractLogger",
  "threadId": 1,
  "threadPriority": 5
}
{noformat}

Proposed requirements:
1. Users would have to pre-escape their JSON string in order to not break 
overall log json format

Implementation approach options:

1. Prefix {noformat}{noesc}{noformat} or other marker macro to instruct 
JSONWriter skip escaping for that string

{noformat}
ThreadContext.push("{noesc}" + JSONString(myModelObject));
{noformat}

_or_

2. Alternatively, the JSONWriter could invoke a JSONReader on the marked 
payload and then ensure that fields are properly escaped, but not escape the 
whole payload. This would involve add'l cycles, but remove the pre-req and 
potential hazard of user calling with malformed JSON

Developer codes:
{noformat}
ThreadContext.push("{json}" + JSONString(myModelObject));
...
JSONWriter detects prefix {json}.. then invokes JSONReader on the payload
{noformat}




> Update JSONTemplateFormat to support not escaping certain payloads
> ------------------------------------------------------------------
>
>                 Key: LOG4J2-3298
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-3298
>             Project: Log4j 2
>          Issue Type: Improvement
>          Components: JsonTemplateLayout
>            Reporter: Matt Pavlovich
>            Assignee: Volkan Yazici
>            Priority: Minor
>
> Currently, if a user's object is able to generate well-formed JSON string, it 
> will be escaped. It would be great if there was a way to config the 
> JsonTemplateLayout to _not escape_ certain payloads. This allows the json to 
> be fully formed and contain model object data that can be parsed out later 
> without any un-formatting. 
> Example log entry:
> {noformat}
> {
>   "instant": {
>     "epochSecond": 1640632561,
>     "nanoOfSecond": 287515000
>   },
>   "thread": "main",
>   "level": "INFO",
>   "loggerName": "io.hyte.dev.c.ServiceC",
>   "message": "Doing way other thing with order: 3235",
>   "contextStack": ["{ \"Task\" {\
>       "id\":\"3623\", \"orderId\": "1345\", \"taskStatus\":null, 
> \"startDateTime\": \"2021-12-27T06:07:01.363232-06:00\", \"endDateTime\": 
> \"2021-12-27T17:10:01.365430-06:00\"} }",
>       "{ \"Order\" { \"id\": \"3235\", \"customerName\": \"Customer-8780\", 
> \"createdDate\" :\"2021-12-27T08:55:01.366162-06:00\"} }"], 
>       
>   "endOfBatch": false, 
>   "loggerFqcn": "org.apache.logging.log4j.spi.AbstractLogger", 
>   "threadId": 1, 
>   "threadPriority": 5
> }
> {noformat}
> Desired output:
> {noformat}
> {
>   "instant": {
>     "epochSecond": 1640632561,
>     "nanoOfSecond": 287515000
>   },
>   "thread": "main",
>   "level": "INFO",
>   "loggerName": "io.hyte.dev.c.ServiceC",
>   "message": "Doing way other thing with order: 3235",
>   "contextStack": [{ "Task" { "id": "3623", "orderId": "1345", 
> "taskStatus":null, "startDateTime": "2021-12-27T06:07:01.363232-06:00", 
> "endDateTime": "2021-12-27T17:10:01.365430-06:00"} }",
>                    { "Order" { "id": "3235", "customerName": "Customer-8780", 
> "createdDate": "2021-12-27T08:55:01.366162-06:00"} }],
>   "endOfBatch": false,
>   "loggerFqcn": "org.apache.logging.log4j.spi.AbstractLogger",
>   "threadId": 1,
>   "threadPriority": 5
> }
> {noformat}
> Proposed requirements:
> 1. Users would have to pre-escape their JSON string in order to not break 
> overall log json format
> Implementation approach options:
> 1. Prefix {noformat}{noesc}{noformat} or other marker macro to instruct 
> JSONWriter skip escaping for that string
> {noformat}
> ThreadContext.push("{noesc}" + JSONString(myModelObject));
> {noformat}
> _or_
> 2. Alternatively, the JSONWriter could invoke a JSONReader on the marked 
> payload and then ensure that fields are properly escaped, but not escape the 
> whole payload. This would involve add'l cycles, but remove the pre-req and 
> potential hazard of user calling with malformed JSON
> Developer codes:
> {noformat}
> ThreadContext.push("{json}" + JSONString(myModelObject));
> ...
> JSONWriter detects prefix {json}.. then invokes JSONReader on the payload
> {noformat}



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to