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

Volkan Yazici commented on LOG4J2-3025:
---------------------------------------

[~ckozak] was spot on with the diagnosis. Though I would like add some personal 
remarks.
h2. Migrate to {{JsonTemplateLayout}}

You should definitely consider migrating to {{JsonTemplateLayout}}, which is 
{{JsonLayout}} successor – not just it is faster, also highly customizable.
h2. Avoid dynamic models in the message field

Many log storage solutions (e.g., Elasticsearch) works with a fixed schema. In 
Elasticsearch, once you index
{code:json}
{
  "message": "AwesomeException (userId=123)"
}
{code}
this will set the type of the {{message}} field to {{string}}. Consequently if 
you try to index
{code:json}
{
  "message": {
    "failure": "AwesomeException",
    "userId": 123
  }
}
{code}
this will fail due to a field type conflict. (There are workarounds for this, 
but I would not recommend any judging from my personal experience.) Hence 
always stick to the same schema while logging structured data.
h2. Avoid polluting the {{message}} field with diagnosis metadata

You are better off using MDC (i.e., context data) fields rather than polluting 
the {{message}} field. Further, in accordance with my remark above regarding 
sticking to the same schema, prefer MDC values of type {{String}}, that is, an 
MDC counterpart of {{Map<String, String>}}.

> log4j2 JsonLayout: In log message, Object is logged as escaped Json
> -------------------------------------------------------------------
>
>                 Key: LOG4J2-3025
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-3025
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Layouts
>    Affects Versions: 2.11.2
>            Reporter: Dee Vinci
>            Assignee: Volkan Yazici
>            Priority: Major
>
> Our organization is using log4j2 version 2.11.2 and xml based configuration, 
> also we are using Elk Stack for further log processing.
> We are trying to move from PatternLayout to Json Layout but facing 2 issues:
> 1.) In Json Layout the object logged in message is logged as escaped json, 
> logging just a simple string works fine, also in PatternLayout proper json is 
> logged
> ----
> Log logged in Json Layout:
>   
> {code:java}
>  {code}
>  
> {code:java}
> {   
> "thread" : "http-nio-8080-exec-2",   "level" : "INFO",   "loggerName" : 
> "***testName***.controller.TestController",   "message" : "Logging testDto 
> \{\"name\":\"testName\",\"address\":\"testAddress\",\"age\":24}
> ",
>   "endOfBatch" : false,
>   "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
>   "instant" :
> {     "epochSecond" : 1614151230,     "nanoOfSecond" : 315000000   }
> ,
>   "contextMap" :
> {     "testId" : " test-2494503368183"   }
> ,
>   "threadId" : 87,
>   "threadPriority" : 5,
>   "@timestamp" : "2021-02-24T12:50:30.315Z",
>   "testId" : " test-2494503368183"
> }
> {code}
>  
>  
> ----
> Expected message is unescaped json, similar to Pattern Layout
>   
>  
> {code:java}
> Logging testDto {"name":"testName","address":"testAddress","age":24}
> {code}
>  
> ----
> As per docs have used
> {code:java}
> objectMessageAsJsonObject="true"{code}
> in JsonLayout configuration property  but message is still logged as escaped 
> json.
>  
> Logs of Diff level are logged in different files, following is the snippet of 
> configuration of Info Logs.
> ----
>  
>  
> {code:java}
>  <RollingFile name="LOG_INFO" fileName="**filepath**/filename_info.log"
>                      
> filePattern="**filepath**/filename_info.%d{yyyy-MM-dd}-%i.gz">
>             <LevelRangeFilter minLevel="INFO" maxLevel="INFO" 
> onMatch="ACCEPT" onMismatch="DENY"/>
>  
>             <!-- <PatternLayout pattern="%d{dd/MM/yyyy HH:mm:ss,SSS} 
> %X{testId} %-5p %c{1} - %.-60000m%n" /> -->
>  
>             <JsonLayout complete="true" compact="false" 
> objectMessageAsJsonObject="true" >
>                 <KeyValuePair key="@timestamp" 
> value="$${date:yyyy-MM-dd'T'HH:mm:ss.SSS'Z'}"/>
>                 <KeyValuePair key="testId" value="$${ctx:testId}"/>
>             </JsonLayout>
>             <Policies>
>                 <TimeBasedTriggeringPolicy />
>                 <SizeBasedTriggeringPolicy size="2 GB"/>
>             </Policies>
> </RollingFile> 
>  
> {code}
>  
> Following is how logger is implemented in codebase
>   
> ---------------------------------------------------------------------------------------------------------
>  
> {code:java}
> private Logger LOG = LogManager.getLogger(TestController.class); // declared 
> at class level
> ‘’’
>  
> ‘’’
>  
> TestDto testDto = new TestDto();
> testDto.setName("testName");
> testDto.setAddress("testAddress");
> testDto.setAge(24);
>  
> LOG.info("Logging testDto {}", testDto);
>  
> {code}
>  
> ----
> Structure of TestDto:
>   
> -----------------------------------------------------------------------------------------------------
>  
> {code:java}
> public class TestDto {
>  
>    private String name;
>    private String address;
>    private Integer age;
>  
>    public String getName()
> {        _return_ name;    }
>  
>    public void setName(String name)
> {        _this_.name = name;    }
>  
>    public String getAddress()
> {        _return_ address;    }
>  
>    public void setAddress(String address)
> {        _this_.address = address;    }
>  
>    public Integer getAge()
> {        _return_ age;    }
>  
>    public void setAge(Integer age)
> {        _this_.age = age;    }
>  
>    @Override
>    public String toString()
> {        _return new_ GsonBuilder().setExclusionStrategies(_new_ 
> CustomExclusionStrategy()).create().toJson(_this_);    
> }
> }
> {code}
>  
> 2.) In Pattern Layout while logging, the prefix string of some of the logs 
> was truncated so we had added {{%.-60000m%}} in pattern Layout configuration 
> so that the beginning of log message is not truncated. Do we have a similar 
> plugin or workaround in Json Layout.
> need to resolve this as early as possible so kindly revert in case of any 
> lead or any extra information needed so that we can resolve this
> Thanks.
> stack overflow link of the same:
> [https://stackoverflow.com/questions/66347969/log4j2-jsonlayout-in-log-message-object-is-logged-as-escaped-json]



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

Reply via email to