SLF4J – Overview

SLF4J – Overview ”; Previous Next SLF4J stands for Simple Logging Facade for Java. It provides a simple abstraction of all the logging frameworks in Java. Thus, it enables a user to work with any of the logging frameworks such as Log4j, Logback and JUL (java.util.logging) using single dependency. You can migrate to the required logging framework at run-time/deployment time. Ceki Gülcü created SLF4J as an alternative to Jakarta commons-logging framework. Advantages of SLF4J Following are the advantages of SLF4J − Using SLF4J framework, you can migrate to the desired logging framework at the time of deployment. Slf4J provides bindings to all popular logging frameworks such as log4j, JUL, Simple logging and, NOP. Therefore, you can switch to any of these popular frameworks at the time of deployment. SLF4J provides support to parameterized logging messages irrespective of the binding you use. Since SLF4J decouples application and logging framework, you can easily write applications independent of logging frameworks. You need not bother about the logging framework being used to write an application. SLF4J provides a simple Java tool known as migrator. Using this tool, you can migrate existing projects, which use logging frame works like Jakarta Commons Logging (JCL) or, log4j or, Java.util.logging (JUL) to SLF4J. Print Page Previous Next Advertisements ”;

SLF4J – Environment Setup

SLF4J – Environment Setup ”; Previous Next 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. 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. In the Java Build Path frame in the Libraries tab, click Add External JARs… Select the slf4j-api.x.x.jar file downloaded and click 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. 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> Print Page Previous Next Advertisements ”;