logo

Log4j 2 YAML Configuration with Console Appender

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.

<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>
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-yaml</artifactId>
    <version>2.9.8</version>
</dependency>

Example YAML configuration to log to Console

Adding /src/main/resources/log4j2.yaml 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 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

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

Happy Coding 😊

Tags: