Home » Enterprise Java » SLF4J » Solving Failed to load class “org.slf4j.impl.StaticLoggerBinder” Error

About Mayank Gupta

Mayank Gupta
Senior JEE developer with experience in large scale IT projects, especially in the telecommunications and financial services sectors. Mayank has been designing and building J2EE applications since 2007. Fascinated by all forms of software development; keen to explore upcoming areas of technology like AI, machine learning, blockchain development and AR. Lover of gadgets, apps, technology and gaming.

Solving Failed to load class “org.slf4j.impl.StaticLoggerBinder” Error

1. Introduction

The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j). This allows the end user to plug in the desired logging framework at deployment time.

SLF4J facilitates your code to make logging calls without binding to a specific logging implementation. This comes real handy for libraries where it is not desired to hard bind the API to a particular logging framework; the program might be using another logging implementation.

2. Failed to load class “org.slf4j.impl.StaticLoggerBinder” Error

While working with SLF4J, you need to provide the following three components (read jars):

  1. SLF4J API (slf4j-api.jar) – This lets you make logging calls to SLF4J’s org.slf4j.Logger in your code (and not a specific logging implementation thus introducing logging abstraction in your code).
  2. SLF4J Binding – This is either slf4j-log4j12.jar (for log4j logging), slf4j-jdk14.jar (for Java logging), slf4j-jcl.jar (if you’re using Commons logging) or logback-classic.jar (for logback logging). With the correct binding jar, SLF4J is able to interface between slf4j-api and underlying logging implementation.
  3. Underlying Logging Implementation – Here you provide the underlying logging library which will actually do the logging for your program. SLF4J will delegate logging calls to this library.

Among all these components, if you miss the 2nd one, then you will run into Failed to load class "org.slf4j.impl.StaticLoggerBinder" message. org.slf4j.impl.StaticLoggerBinder is found in any of the SLF4J’s binding jar.

Let’s try some code and how to fix this.

3. Example

3.1 Create a Maven Project

We will create a bare minimum Maven project. Once you have the Maven installed and running on your machine, issue the following command from the command line.

mvn archetype:generate -DgroupId=com.javacodegeeks -DartifactId=slf4-spring-boot -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

In this example, Maven will manage dependencies for us and we don’t need to download any jars explicitly.

3.2 Add Maven Dependencies

Add dependency just for slf4j-api.

pom.xml

<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>com.javacodegeeks</groupId>
  <artifactId>slf4jconfig-javalogging</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>slf4jconfig-javalogging</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-api</artifactId>
    	<version>1.7.25</version>
    </dependency>
  </dependencies>
</project>

3.3 Test Class

HelloJavaLogging.java

package com.javacodegeeks.slf4jconfig_javalogging;

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

/**
 * HelloJavaLogging!
 *
 */
public class HelloJavaLogging 
{
    public static void main( String[] args )
    {
    	Logger logger = LoggerFactory.getLogger(HelloJavaLogging.class);
        logger.info("This is how you configure Java Logging with SLF4J");
    }
}

Run the code.

Output:

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.

Seems like we missed the binding jar for SLF4J and Java Logging. Let’s add slf4j-jdk14 jar file and see if it solves the problem.

Add the following lines in your pom.xml:

<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-jdk14</artifactId>
	<version>1.7.25</version>
</dependency>

NOTE: We don’t need to provide the underlying logging framework’s jar in this case as we are using Java Logging. The implementation is provided in JDK.

Let’s re-run the code.

Output:

May 03, 2017 8:10:40 PM com.javacodegeeks.slf4jconfig_javalogging.HelloJavaLogging main
INFO: This is how you configure Java Logging with SLF4J

Finally we got the output we desired.

Remember, the absence of SLF4J Binding runs into Failed to load class “org.slf4j.impl.StaticLoggerBinder” Error.

NOTE:

  1. As of SLF4J version 1.6, in the absence of a binding, SLF4J will default to a no-operation (NOP) logger implementation.
  2. As of SLF4J version 1.8.0, the static binder mechanism will be deprecated. StaticLoggerBinder is likely to be removed in future releases of SLF4J.

4. Download the Eclipse Project

Download
You can download the full source code of this example here: slf4jconfig-javalogging
(+2 rating, 2 votes)
6 Comments Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

6
Leave a Reply

avatar
6 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
Carlos CascaAnudeepIoannisJituRalph Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Ragesh D Antony
Guest
Ragesh D Antony

Thank you. You solved my problem

Ralph
Guest
Ralph

Thank you :) ! I have been stuck on it for a while!

Jitu
Guest

Great job explaining this

Ioannis
Guest
Ioannis

Thank you :D

Anudeep
Guest
Anudeep

Thank you, You solved my problem.

Carlos Casca
Guest
Carlos Casca

Thank you, You solved my problem.