SLF4J - Environment Setup


Advertisements

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

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 project and set build path

Open eclipse and create a sample project. Right-click on the project, select the option Build Path → Configure Build Path… as shown below.

Project And Set Build Path

In the Java Build Path frame in the Libraries tab, click Add External JARs…

Java Build Path

Select the slf4j-api.x.x.jar file downloaded and click Apply and Close.

Apply And Close

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.

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.

Pom.xml for SLF4J

If you are creating the maven project, 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
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>Sample</groupId>
   <artifactId>Sample</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <build>
      <sourceDirectory>src</sourceDirectory>
      <plugins>
         <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
               <source>1.8</source>
               <target>1.8</target>
            </configuration>
         </plugin>
      </plugins>
   </build>
   <dependencies>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
         <version>1.7.25</version>
      </dependency>
   </dependencies>
</project>
Advertisements