Log4j 2 YAML Configuration with Console Appender

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

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 YAML configuration file to log messages to sytem console using Console appender.

Adding Log2j 2 Dependencies to your project

In order to read YAML configuration file Log4j 2 using Jackson and jackson-dataformat-yaml libraries so that you need to add these 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'
    compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.9.8'

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


Example YAML configuration to log to Console

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

  status: error
  name: ConsoleExample
  packages: "simplesolution.dev"
      name: STDOUT
        Pattern: "%d - %t - %p - %c - %m%n"
      level: trace
        ref: STDOUT

Example code to log messages

package simplesolution.dev;

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

public class Log4j2YAMLConfigurationConsoleExample {

    private static final Logger logger = LogManager.getLogger(Log4j2YAMLConfigurationConsoleExample.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:43:12,420 - main - TRACE - simplesolution.dev.Log4j2YAMLConfigurationConsoleExample - Trace message
2019-05-08 00:43:12,427 - main - INFO - simplesolution.dev.Log4j2YAMLConfigurationConsoleExample - Info message
2019-05-08 00:43:12,427 - main - DEBUG - simplesolution.dev.Log4j2YAMLConfigurationConsoleExample - Debug message
2019-05-08 00:43:12,427 - main - WARN - simplesolution.dev.Log4j2YAMLConfigurationConsoleExample - Warn message
2019-05-08 00:43:12,428 - main - ERROR - simplesolution.dev.Log4j2YAMLConfigurationConsoleExample - Error message
2019-05-08 00:43:12,428 - main - FATAL - simplesolution.dev.Log4j2YAMLConfigurationConsoleExample - Fatal message

Download Source Code

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

Happy Coding 😊