Logging with SLF4J

Logging is important in software development. There are many famous frameworks to be used for logging: Log4j, Logback, java.util.logging… If we wanna change the logging framework without affecting Java code, SLF4J is the good choice.

This tutorial writes about SLF4J with example of how to apply SLF4J in some logging frameworks.

I. What is SLF4J

SLF4J, the Simple Logging Facade for Java, serves as a simple facade or abstraction for many logging frameworks above. The layer we apply logging is independent from logging implementation. We only have to choose the configuration of logging system.

slf4j-layer

To work with SLF4J, we add its dependency to our Maven Project pom file:

		
			org.slf4j
			slf4j-api
			1.7.21
		

SLF4J API is easy to use, this is how we get a logger:

static Logger logger = LoggerFactory.getLogger(MainApp.class);

And this is a very simple example of using that Logger:

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

public class MainApp {

	static Logger logger = LoggerFactory.getLogger(MainApp.class);
	
	public static void main(String[] arg) {

		logger.info("Hello World");

		System.err.println("Java Sample Approach");
	}
}

Run the project, the console window shows:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Java Sample Approach

This warning is printed because no slf4j binding could be found on the class path. It lacks of logging implementation.

slf4j-layer-null

We can apply the native implementation of slf4j-api: slf4j-simple or slf4j-nop
slf4j-layer-simple-nop

For slf4j-nop, add this dependency after slf4j-api dependency:

		
			org.slf4j
			slf4j-nop
			1.7.21
		

Run the project, the console window shows only:

Java Sample Approach

For slf4j-simple, add this dependency after slf4j-api dependency:

		
			org.slf4j
			slf4j-simple
			1.7.21
		

Run the project, the console window shows an additional simple logging:

[main] INFO MainApp - Hello World
Java Sample Approach

Notice that, If we use more than one loggin implementation, SLF4J will emit a warning with listing the location of those bindings:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/TienTN/.m2/repository/org/slf4j/slf4j-nop/1.7.21/slf4j-nop-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/TienTN/.m2/repository/org/slf4j/slf4j-simple/1.7.21/slf4j-simple-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.helpers.NOPLoggerFactory]

It is because SLF4J API is designed to bind with ONE and only ONE underlying logging framework at a time.

II. Supported Frameworks
1. Logback

slf4j-layerlogback
Add this dependency after slf4j-api dependency:

		
			ch.qos.logback
			logback-classic
			1.0.13
		

The Dependency Hierarchy after that is:
slf4j-dependency-logback
Run the project, the console window shows:

00:01:23.011 [main] INFO  MainApp - Hello World
Java Sample Approach
2. java.util.logging

slf4j-layer-jdk14
Add this dependency after slf4j-api dependency:

		
			org.slf4j
			slf4j-jdk14
			1.7.21
		

The Dependency Hierarchy after that is:
slf4j-dependency-jdk14
Run the project, the console window shows:

Nov 21, 2016 12:12:29 AM MainApp main
INFO: Hello World
Java Sample Approach
3. Log4j

slf4j-layer-log4j12
Add this dependency after slf4j-api dependency:

		
			org.slf4j
			slf4j-log4j12
			1.7.21
		

The Dependency Hierarchy after that is:
slf4j-dependency-log4j12
Run the project, the console window shows:

log4j:WARN No appenders could be found for logger (MainApp).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Java Sample Approach

It is because we haven’t set configuration for Log4j.
To do this, we can look back to the post How to configure Apache Log4j2 with XML Configuration.
Or create a configuration file (reference from http://logging.apache.org/log4j/1.2/manual.html)

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Run the project again, the console window shows:

1    [main] INFO  MainApp  - Hello World
Java Sample Approach
III. Source code
1. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE (It’s OK if you use Eclipse)

2. Download Link

loggingslf4j

3. Check results

– Config maven build:
clean install
– Open pom.xml file and modify
– Run project with mode Java Application



By grokonez | November 20, 2016.

Last updated on June 4, 2017.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*