Log4j2 – Maven and Gradle Configuration

Learn to configure Log4j2 using Maven and Gradle dependencies.

1. Log4j Modules

The main modules of interest are given below.

  • log4j-api – provides the adapter components required for implementers to create a logging implementation.
  • log4j-core – core Log4j Implementation classes.
  • log4j-slf4j-imp – allows applications coded to the SLF4J API to use Log4j2 as the implementation.
  • log4j-jcl – provides a bridge for Apache commons-logging.
  • log4j-jul – adapter for Java utility logging (JUL).
  • log4j-web – provides support for automatically enabling Log4j in Servlet containers.

We can read the complete list of modules and their dependencies at this link.

2. Maven Dependencies

An example of log4j2 configuration with SLF4J using Maven. We can check the latest version of log4j2 in its Maven dependency page.

<properties>
    <log4j2.version>2.20.0</log4j2.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${log4j2.version}</version>
    </dependency>
</dependencies>

3. Gradle Dependencies

An example of log4j2 configuration using Gradle.

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
    implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
    implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.20.0'
}

After modifying the build.gradle file, sync the Gradle project to download the new dependencies. We can do this in the IDE or by running the following command in the terminal:

./gradlew build

4. log4j2.xml

Once the dependencies are downloaded, we can start using Log4j2 in our Java code. Create a log4j2.xml configuration file in the src/main/resources directory. This is a simple example:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

5. Using Logger in Application Code

We can use the LogManager API to bootstrap a Logger instance as follows:

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

public class Main {

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

	public static void main(final String... args) 
        {
	    logger.info("Hello Logging!");
	}
}

If we are using Log4j2 with SLF4j then we can use the LoggerFactory api.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {

  private static final Logger logger = LoggerFactory.getLogger(Main.class);

  public static void main(final String[] args)
  {

      logger.info("Hello Logging!");
  }
}

Let me know your thoughts in the comments or feedback.

Happy Learning !!

Comments

Subscribe
Notify of
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.