Log4j2 with SLF4J Configuration

Learn to configure Log4j2 logging with SLF4J APIs. We will look at required dependencies, sample configuration and a demo to use the log statements.

Read More: SLF4j Vs Log4j – Which One is Better?

1. Log4j2 and SLF4j Binding Dependencies

To make Log4j2 work with SLF4J, we need to include the following 3 dependencies. Click on the respective links to get the latest version of each.

  • log4j-slf4j-impl.jar – Log4j 2 SLF4J binding. It allows applications coded to the SLF4J API to use Log4j2 as the implementation.
  • log4j-api.jar – provides the adapter components required for implementers to create a logging implementation.
  • log4j-core.jar – core Log4j Implementation classes.

The Maven and Gradle dependencies can be copied as below.

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.15.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.15.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.15.0</version>
</dependency>
dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.15.0'
  compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.15.0'
  compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.15.0'
}

2. Logging Configuration

Next is to provide a log4j2.properties, log4j2.xml or log4j2.json file which will configure the required loggers and appenders. Place the configuration file are in the resources folder or application classpath. All log statements will log using these loggers.

We are taking the example for XML configuration and using console logging for demo purposes. You can use one of the other useful logging patterns as well.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n</Property>
    </Properties>
 
    <Appenders>
        <Console name="console" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Console>
    </Appenders>
 
    <Loggers>
        <Root level="info">
            <AppenderRef ref="console"/>
        </Root>
    </Loggers>
</Configuration>

3. Demo

Write the log statements in the application code with classes Logger and LoggerFactory. Both classes come from the package org.slf4j.

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

public class Main {
  public static void main(final String[] args)
  {
      Logger logger = LoggerFactory.getLogger(Main.class);
      logger.info("Hello World !!");
  }
}

Run the main() method and observe the output in the console.

2021-12-13 22:08:14 INFO  Main - Hello World !!

That’s all for log4j2 with slf4j configuration example.

Happy Learning !!

Download Sourcecode

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.