Log4j 2 JSON Configuration with Console Appender

Tags: Log4j Apache Log4j 2 log4j-api log4j-core log4j LogManager log4j Logger log4j json jackson

Java Code Examples for using

  • org.apache.logging.log4j.LogManager
  • org.apache.logging.log4j.Logger

The Java code example below to show you how to configure Log4j 2 with JSON configuration file to log messages to sytem console using Console appender.

Adding Log2j 2 Dependencies to your project

In order to read JSON configuration file Log4j 2 using Jackson library so that you need to add Jackson dependencies to your project. Add dependencies below to your build.gradle if you are using Gradle build.

dependencies {
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.2'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.2'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.8'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.8'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.8'
}

Add dependencies below to your pom.xml if you are using Maven build.

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.11.2</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.8</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.8</version>
</dependency>

Example JSON configuration to log to Console

Adding /src/main/resources/log4j2.json to your project

{
  "configuration": {
    "status": "error",
    "name": "ConsoleExample",
    "packages": "simplesolution.dev",
    "appenders": {
      "Console": {
        "name": "STDOUT",
        "PatternLayout": {
          "pattern": "%d - %t - %p - %c - %m%n"
        }
      }
    },
    "loggers": {
      "root": {
        "level": "trace",
        "AppenderRef": {
          "ref": "STDOUT"
        }
      }
    }
  }
}

Example code to log messages

package simplesolution.dev;

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

public class Log4j2JSONConfigurationConsoleExample {

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

    public static void main(String... args) {
        logger.trace("Trace message");
        logger.info("Info message");
        logger.debug("Debug message");
        logger.warn("Warn message");
        logger.error("Error message");
        logger.fatal("Fatal message");
    }
}

Application output:

2019-05-08 00:51:47,834 - main - TRACE - simplesolution.dev.Log4j2JSONConfigurationConsoleExample - Trace message
2019-05-08 00:51:47,844 - main - INFO - simplesolution.dev.Log4j2JSONConfigurationConsoleExample - Info message
2019-05-08 00:51:47,844 - main - DEBUG - simplesolution.dev.Log4j2JSONConfigurationConsoleExample - Debug message
2019-05-08 00:51:47,844 - main - WARN - simplesolution.dev.Log4j2JSONConfigurationConsoleExample - Warn message
2019-05-08 00:51:47,844 - main - ERROR - simplesolution.dev.Log4j2JSONConfigurationConsoleExample - Error message
2019-05-08 00:51:47,845 - main - FATAL - simplesolution.dev.Log4j2JSONConfigurationConsoleExample - Fatal message

Download Source Code

The source code in this article can be found at: https://github.com/simplesolutiondev/Log4j2JSONConfigurationConsole

Happy Coding 😊