Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Not A Problem
-
2.11.2
-
None
-
None
Description
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:
{ "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 {"name":"testName","address":"testAddress","age":24}
As per docs have used
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.
<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
---------------------------------------------------------------------------------------------------------
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:
-----------------------------------------------------------------------------------------------------
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_); } }
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: