Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add json object to log4j2 when is JSONLayout

Tags:

java

json

log4j2

I will show log4j2 with JSONLayout same as object on messages. for example my config of is:

cat log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
    <File name="Json" fileName="/home/jeus/log/loggerjson/main.log" bufferedIO="true" advertiseURI="file://home/jeus/log/loggerjson/main1.log" advertise="true">
        <JSONLayout compact="true" locationInfo="true" complete="false" eventEol="true" properties="true" propertiesAsList="true"/>
    </File>       
</Appenders>
<Loggers>
    <Root level="info">
        <AppenderRef ref="Json"/>
    </Root>
</Loggers>

my out put Output:

cat /home/jeus/log/loggerjson/main.log

{
 "timeMillis":1502359773290,
 "thread":"main",
 "level":"INFO",
 "loggerName":"com.jeus.logger.json.loggerjson.Main",
 "message":"This message is a raw",
 "endOfBatch":false,
 "loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger",
 "contextMap":[  ],
 "threadId":1,
 "threadPriority":5,
 "source":{  
     "class":"com.jeus.logger.json.loggerjson.Main",
     "method":"main",
     "file":"Main.java",
     "line":61
      }
}

i add a json object to log but not show the json object in messages and show that with \" charecter

JSON object:

{"line_id": 12,"play_name":"Jeus"}

my code to log:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Main {

       private static final Logger LOGGER = LogManager.getLogger(Main.class);

       public static void main(String[] args) {


            String message = "{\"line_id\": 12,\"play_name\": \"Jeus\"}";
            LOGGER.info(message);

        }
 }

output is:

{  
 "timeMillis":1502361394238,
 "thread":"main",
 "level":"INFO",
 "loggerName":"com.jeus.logger.json.loggerjson.Main",
 "message":"{\"line_id\": 12,\"play_name\": \"Jeus\"}",
 "endOfBatch":false,
 "loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger",
 "contextMap":[  

 ],
 "threadId":1,
 "threadPriority":5,
 "source":{  
    "class":"com.jeus.logger.json.loggerjson.Main",
    "method":"main",
    "file":"Main.java",
    "line":62
    }
 }

but i will show message as a json object same this:

 "message":{"line_id": 12,"play_name":"Jeus"},
like image 488
Jeus Avatar asked Nov 08 '22 18:11

Jeus


1 Answers

This doesn't seem possible to do with the built-in JsonLayout. No matter what I try it just does a toString on the field rather than serializing it correctly.

One solution is to use the PatternLayout and format it like JSON.

log4j.appender.frontEndAudit.layout=org.apache.log4j.PatternLayout
log4j.appender.frontEndAudit.layout.ConversionPattern={"timestamp": 
"%d{MM/dd/yyyy HH:mm:ss:SSSS}", "class": "%C", "file": "%F", "level" : 
"%5p", "line_number" : "%L", "logger_name": "frontEndAuditLog", "mdc": 
"ipAddress": "%X{ipAddress}", "requestId":"%X{requestId}", 
"sessionId":"%X{sessionId}"}, "message": %m, "method": "%M", 
"source_host":"%X{sourceHost}", "thread_name": "%t" }%n

This was for log4j1 but same concept would work for log4j2

like image 188
Spencer Sutton Avatar answered Nov 15 '22 01:11

Spencer Sutton