Getting started with Log4j2

Log4j2 is the upgraded version of Apache Log4j with significant improvements over its predecessor and provides many of the improvements available in Logback while fixing some inherent problems occurred in Logback’s architecture such as performance improvement, hot/automatic reloading once configuration files modified, Java 8 lambda support and custom log levels. We had already discussed how to use Log4j to manage log messages in your Java program fundamentally. Thus, this post will lead you to get started with Log4j2 and then present some of these improvements in a nutshell.

[box type=”info” ]Log4j 2.4 and greater requires Java 7. Versions 2.0-alpha1 to 2.3 required Java 6.[/box]

Log4j2 dependencies

To use Log4j2 in your project, you have to declare along those lines of the following snippet in your build file. Here are the instructions for Maven and Gradle based projects.

Using Log4j in Apache Maven build

To build with Apache Maven, add the dependencies listed below in pom.xml  file.

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.6.1</version>
</dependency>

Using Log4j in Gradle build

To build with Gradle, add the dependencies listed below in build.gradle  file.

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'
}

[box type=”info” ]For dependency declaration in other build tools, please consult section Maven, Ivy, Gradle, and SBT Artifacts in Log4j’s official documentation.[/box]

Log4j2 configuration file

Log4j supports multiple formats for configuration files. You freely go with log4j2.{xml, json, properties}. See below an example of log4j2.xml.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="debug" additivity="false">
            <AppenderRef ref="console" />
        </Root>
    </Loggers>
</Configuration>

If you want to tell Log4j2 with log4j2.json, you need to add the dependencies listed below in pom.xml or build.gradle.

Maven

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

Gradle

// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'

You should leave the configuration file anywhere in the application’s classpath. The common location is src/main/java/resources as shown in the screenshot below.

An illustrated program

You might prefer a small program to demonstrate how Log4j2 works. Here you are.

package com.itersdesktop.javatechs;

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

public class Log4j2Usage {
    private static final Logger logger = LogManager.getLogger(Log4j2Usage.class.getName());
    public static void main(String[] args) {
        logger.info("Info: Log4j2 Usage");
        logger.debug("Debug: Program has finished successfully");
        logger.error("Error: Program has errors");
    }
}

Run the petite program above, you will see the output along those lines of the following screenshot.

The source code of the sample project can be found in bitbucket.

We hope you enjoy our post. Please give your comments into feedback section.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.