Saturday 17 October 2015

Log4j2: JSONLayout: Store log events in JSON fromat

By using JSONLayout, you can store log events in JSON format. This layout requires Jackson jar files in your project classpath.
Following are the maven dependencies, I used.
<dependencies>
 <dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-api</artifactId>
  <version>2.4</version>
 </dependency>
 <dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.4</version>
 </dependency>
 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.6.2</version>
 </dependency>
</dependencies>


log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
 <Appenders>
  <File name="my_file_appender" fileName="/Users/harikrishna_gurram/log.json">
   <JSONLayout />
  </File>
 </Appenders>

 <Loggers>
  <Root level="info">
   <AppenderRef ref="my_file_appender" />
  </Root>
 </Loggers>
</Configuration>

package log4j_tutorial;

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

public class HelloWorld {
 private static final Logger logger = LogManager.getLogger();

 public static void main(String args[]) throws InterruptedException {
  int val1 = 10, val2 = 11, val3 = 12;

  logger.trace("val1={}, val2={}, val3={}", val1, val2, val3);
  logger.debug("val1={}, val2={}, val3={}", val1, val2, val3);
  logger.info("val1={}, val2={}, val3={}", val1, val2, val3);
  logger.warn("val1={}, val2={}, val3={}", val1, val2, val3);
  logger.error("val1={}, val2={}, val3={}", val1, val2, val3);
  logger.fatal("val1={}, val2={}, val3={}", val1, val2, val3);

 }

}


Run HelloWorld application, you will get following output in ‘log.json’ file.

$ cat log.json 
{
  "timeMillis" : 1444024400438,
  "thread" : "main",
  "level" : "INFO",
  "loggerName" : "log4j_tutorial.HelloWorld",
  "message" : "val1=10, val2=11, val3=12",
  "endOfBatch" : false,
  "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger"
}
{
  "timeMillis" : 1444024400510,
  "thread" : "main",
  "level" : "WARN",
  "loggerName" : "log4j_tutorial.HelloWorld",
  "message" : "val1=10, val2=11, val3=12",
  "endOfBatch" : false,
  "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger"
}
{
  "timeMillis" : 1444024400511,
  "thread" : "main",
  "level" : "ERROR",
  "loggerName" : "log4j_tutorial.HelloWorld",
  "message" : "val1=10, val2=11, val3=12",
  "endOfBatch" : false,
  "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger"
}
{
  "timeMillis" : 1444024400511,
  "thread" : "main",
  "level" : "FATAL",
  "loggerName" : "log4j_tutorial.HelloWorld",
  "message" : "val1=10, val2=11, val3=12",
  "endOfBatch" : false,
  "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger"
}

Following are the JSON Layout Parameters.
Parameter
Type
Description
charset
String
The character set to use when converting the HTML String to a byte array. Default is UTF-8.
compact
boolean
Default is false. If true, the appender does not use end-of-lines and indentation.
eventEol
boolean
Default is false. If true, the appender appends an end-of-line after each record. Use with eventEol=true and compact=true to get one record per line.
complete
boolean
Default is false. If true, the appender includes the JSON header and footer.
properties
boolean
Default is false. If true, the appender includes the thread context in the generated JSON.
locationInfo
boolean
Default is false. If true, the appender includes the location information in the generated JSON. Generating location information is an expensive operation and may impact performance. Use with caution.




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment