Log4j 2 - JSON configuration example



Log4j 2 - JSON configuration example

In this post, you will learn how to configure the Log4j 2 API in a java application using JSON configuration file.

Tools and technologies used for this example are-

  • JDK 1.8.0_121
  • Maven 3.3.9
  • Eclipse Neon.3
  • Log4j 2.8.2
  • Jackson API 2.8.7

 

Project structure

Review the following maven project structure.

Log4j 2 - JSON configuration example | BORAJI.COM

Refer this article to learn - How to create a maven project in eclipse.

 

Jar dependencies

Edit pom.xml file and add log4j2 and Jackson API dependencies in it as follows.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.boraji.tutorial.log4j2</groupId>
  <artifactId>log4j2-json-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>log4j2-json-example</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.8.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.8.2</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.8.7</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.8.7</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.8.7</version>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

 

JSON configuration file for Log4j 2

Create a log4j2.json file under src/main/resources source folder and write the following code in it.

log4j2.json

{
   "Configuration": {
      "Appenders": {
         "Console": {
            "PatternLayout": {
               "pattern": "%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n"
            },
            "name": "Console",
            "target": "SYSTEM_OUT"
         },
         "File": {
            "PatternLayout": {
               "pattern": "%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n"
            },
            "name": "File",
            "fileName": "c:/log/app.log"
         }
      },
      "Loggers": {
         "Root": {
            "AppenderRef": [
               {
                  "ref": "Console"
               },
               {
                  "ref": "File"
               }
            ],
            "level": "trace"
         }
      }
   }
}

Console Appender - Writes the logs to either System.out or System.err.

File Appender - Writes the logs to the File named in the fileName parameter.

View an equivalent - Log4j 2 XML configuration file.

View an equivalent - Log4j 2 YAML configuration file.

 

Test Log4j 2 configuration

Create a main class to test the above log4j 2 configuration.

MainApp.java

package com.boraji.tutorial.log4j2;

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

public class MainApp {

   private static final Logger logger = LogManager.getLogger(MainApp.class);

   public static void main(String[] args) {

      logger.trace("Entering application...");

      logger.info("Hello Log4j2...");
      logger.error("Something is wrong with this code", new Exception("Invalid message"));

      logger.trace("Exiting application...");
   }
}

After executing the MainApp.java the console output and c:/log/app.log file will look like as follows.

2017-May-01 21:44:18 PM [main] TRACE com.boraji.tutorial.log4j2.MainApp - Entering application...
2017-May-01 21:44:18 PM [main] INFO  com.boraji.tutorial.log4j2.MainApp - Hello Log4j2...
2017-May-01 21:44:18 PM [main] ERROR com.boraji.tutorial.log4j2.MainApp - Something is wrong with this code
java.lang.Exception: Invalid message
	at com.boraji.tutorial.log4j2.MainApp.main(MainApp.java:15) [classes/:?]
2017-May-01 21:44:18 PM [main] TRACE com.boraji.tutorial.log4j2.MainApp - Exiting application...

 

 

Download Source: