简体   繁体   中英

Add json object to log4j2 when is JSONLayout

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

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

It turns out LogstashLayout is a custom layout that does the trick.

  1. Write "message": "${json:message:json}" in LogstashJsonEventLayoutV1.json file.
  2. You have to log a Java object which implements the MultiformatMessage and return the JSON string in getFromattedMessage method.
  3. getMessageFormats() method also must return JSON .

log4j-layout-template-json is available with version 2.14.0 and works flawlessly using MapMessage.Just add below gradle dependencies if using gradle

compile 'org.apache.logging.log4j:log4j-api:2.14.0'
compile 'org.apache.logging.log4j:log4j-core:2.14.0'
compile 'org.apache.logging.log4j:log4j-layout-template-json:2.14.0'
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.10.0'
compile 'com.fasterxml.jackson.dataformat:jackson-databind:2.10.0'

log4j2.yaml looks like

Configuration:
  name: Default
  Appenders:
   Console:
    name: LogToConsole
    target: SYSTEM_OUT    
    JsonTemplateLayout:
      eventTemplateUri: "classpath:LogstashJsonEventLayoutV1.json"
  Loggers:
   AsyncRoot: 
     level: info
     additivity: false
     AppenderRef:
         - ref: LogToConsole

Then extend MapMessage for the object(MyMapMessage) you want to log and use

private static final org.apache.logging.log4j.Logger LOGGER = LogManager.getLogger(YourClass.class);
LOGGER.error(myMapMessage.getData());

• In log4j2.xml, set the objectMessageAsJsonObject option:

<JSONLayout objectMessageAsJsonObject="true" [...] />

• Use ObjectMessage (sample code, with jackson):

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import org.apache.logging.log4j.*;
import org.apache.logging.log4j.message.ObjectMessage;

JsonNodeFactory f = JsonNodeFactory.instance;
JsonNode event = f.objectNode()
    .put("eventName", "abc")
    .set("person", f.objectNode()
        .put("firstName", "John")
        .put("lastName", "Doe")
    );

// "message":{"eventName":"abc","person":{"firstName":"John","lastName":"Doe"}}
logger.info(new ObjectMessage(event));

// "message":"{\"eventName\":\"abc\",\"person\":{\"firstName\":\"John\",\"lastName\":\"Doe\"}}"
logger.info(new ObjectMessage(event.toString()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM