SLF4J - Environment Setup



In this chapter, we will explain how to set SLF4J environment in Eclipse IDE. Before proceeding with the installation, make sure that you already have Eclipse installed in your system. If not, download and install Eclipse.

For more information on Eclipse, please refer our Eclipse Tutorial

Step 1: Download the dependency JAR file

Open the official homepage of the SLF4J website and go to the download page.

SLF4J Homepage

or download from Maven Repository slf4j-api

Now, download the latest stable version of slf4j-X.X.tar.gz or slf4j-X.X.zip, according to your operating system (if windows .zip file or if Linux tar.gz file).

Within the downloaded folder, you will find slf4j-api-X.X.jar. This is the required Jar file.

Step 2: Create a Maven project and set build path

Open eclipse and create a Maven based project as shown below.

New Maven Project

Update the pom.xml as shown below. Rest eclipse will handle automatically.

Pom.xml for SLF4j

open the pom.xml and paste the following content in it and refresh the project.

<project xmlns="http://maven.apache.org/POM/4.0.0" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>sl4j</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>SL4JDemo</name>
   <build>      
      <plugins>
         <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
               <source>24</source>
               <target>24</target>
            </configuration>
         </plugin>
      </plugins>
   </build>
   <dependencies>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
         <version>2.1.0-alpha1</version>
      </dependency>
   </dependencies>
</project>

SLF4J Bindings

In addition to slf4j-api.x.x.jar file, SLF4J provides several other Jar files as shown below. These are called SLF4J bindings.

Where each binding is for its respective logging framework.

The following table lists the SLF4J bindings and their corresponding frameworks.

Sr.No Jar file & Logging Framework
1

slf4j-nop-x.x.jar

No operation, discards all loggings.

2

slf4j-simple-x.x.jar

Simple implementation where messages for info and higher are printed and, remaining all outputs to System.err.

3

slf4j-jcl-x.x.jar

Jakarta Commons Logging framework.

4

slf4j-jdk14-x.x.jar

Java.util.logging framework (JUL).

5

slf4j-log4j12-x.x.jar

Log4J frame work. In addition, you need to have log4j.jar.

To make SLF4J work along with slf4l-api-x.x.jar, you need to add the respective Jar file (binding) of the desired logger framework in the classpath of the project (set build path).

To switch from one framework to other, you need to replace the respective binding. If no bounding is found, it defaults to no-operation mode.

Advertisements