Dee Vinci created LOG4J2-3025:
---------------------------------

             Summary: 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


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}
{{{ }}
{{"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" }}
{{}}}{{}}

 
----
Expected message is unescaped json, similar to Pattern Layout
 
{{Logging testDto }}
{code:java}

{code}
{{{"name":"testName","address":"testAddress","age":24}}}{{}}

 
----
As per docs have used {{}}
{code:java}

{code}
{{objectMessageAsJsonObject="true"}} 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}

{code}
{{<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>}}{{}}

 

Following is how logger is implemented in codebase
 
{{}}
{code:java}

{code}
{{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);}}{{}}

 
----
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...|https://stackoverflow.com/users/1278899/volkan-yaz%C4%B1c%C4%B1]



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

Reply via email to