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

Ralph Goers commented on LOG4J2-3025:
-------------------------------------

[~DeeVinci01] Since you asked for my input here it is.

First, I would recommend that if you already haven't you review 
[http://logging.apache.org/log4j/2.x/manual/cloud.html|http://logging.apache.org/log4j/2.x/manual/cloud.html.].
 I've done quite a bit of testing and was not successful in getting logs with 
stacktraces to log through ELK correctly without using Gelf. Terminating log 
events as they pass through Logstash is critical for properly formatting the 
events and the logstash gelf plugin seems to be about the only thing that does 
that well. You can use either of Log4j's GelfLayout or JsonTemplateLayout to 
generate the events. JsonTemplateLayout is more flexibile.

As for your data if I were you I would create a custom MessageFactory. Create a 
custom message that extends ParameterizedMessage and checks the argument(s) for 
your DTO. If it finds it then format the message and object as valid json. If 
not, delegate to ParameterizedMessage.

> 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