Search Tutorials


SL4J Tutorial | JavaInUse



Configure SLF4J with the available logging frameworks



What is SLF4J?
SLF4J is a simple facade for logging systems allowing the end-user to plug-in the desired logging system at deployment time. SLF4J is only a facade, meaning that it does not provide a complete logging solution. It provides a common interface for various Java loggers. So it is used with actual loggers which actually log the events. Examples of such loggers are Logback, Apache Commons Logging, JDK 1.4 Logging or Log4j. Simple Logging Facade for Java (SLF4J) is an abstraction of different logging frameworks (eg. log4j, java.util.logging, commons logging etc.). This gives the developer an opportunity to plug-in desired logging framework according to the requirement at deployment time without changing the code.


java11_1

We will test the following SLF4JConfigTest with various implementations of slf4j.
package com.javainuse;

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

public class SLF4JConfigTest {
	
	static Logger LOGGER = LoggerFactory.getLogger(SLF4JConfigTest.class);
	
	public static void main(String args[])
	{
		LOGGER.debug("hello world");
	}

}
	

Configuration SLF4J and SLF4J NOP

Add the following Maven Dependency
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
Currently only the slf4j-api is added but no implementation of slf4j is added. On running the above SLF4JConfigTest we get the output as-
java11_nop
So we cannot see the logged information and only the message is displayed that no implementation of SLF4J Api is provided.

Configuration SLF4J using Simple Logger

Add the following Maven Dependency
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency> 

Here we added slf4j-simple implementation of slf4j-api. Simple implementation that does not require any configuration at all. On running the above SLF4JConfigTest we get the output as-
java11_simple

Configuration SLF4J using Java Logger

Add the following Maven Dependency
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.21</version>
</dependency>

Here we added slf4j-jdk14 implementation of slf4j-api. So in this case 'slf4j-jdk14-1.6.5.jar' will act as an adapter between SLF4J API and actual logging framework. On running the above SLF4JConfigTest we get the output as-
java11_jdk

Configuration SLF4J using Logback

Add the following Maven Dependency
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>

Here we added slf4j-log4j12 implementation of slf4j-api. It will transitively add logback-core dependency. On running the above SLF4JConfigTest we get the output as-
java11_logback

Configuration SLF4J using Log4J

Add the following Maven Dependency
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>

Here we added logback-classic implementation of slf4j-api. It acts as an adapter between SLF4J API and actual logging framework For using Log4J we have to load the basic configuration. So there is a minor addition in program as follows-
package com.javainuse;

import org.apache.log4j.BasicConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SLF4JConfigTest {
	
	BasicConfigurator.configure();
	
	static Logger LOGGER = LoggerFactory.getLogger(SLF4JConfigTest.class);
	
	public static void main(String args[])
	{
		LOGGER.info("hello world");
	}

}
On running the above SLF4JConfigTest we get the output as-
java11_log4j