In this post, we feature a comprehensive article about how to solve the error slf4j: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.
You can also check this tutorial in the following video:
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. The slf4j: Failed to load class “org.slf4j.impl.StaticLoggerBinder” Error
While working with SLF4J, you need to provide the following three components (read jars):
- SLF4J API (slf4j-api.jar) – This lets you make logging calls to SLF4J’s
org.slf4j.Loggerin your code (and not a specific logging implementation thus introducing logging abstraction in your code).
- 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.
- 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 the Failed to load class “org.slf4j.impl.StaticLoggerBinder” message and the org.slf4j.impl.StaticLoggerBinder is found in any of the SLF4J’s binding jar.
Let’s try some code and see how we can fix this.
3. Example of the error Failed to load class “org.slf4j.impl.StaticLoggerBinder” in slf4j
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.
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.
3.3 Test Class
Run the code.
It 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:
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.
Finally, we got the output we desired.
4. slf4j: Failed to load class “org.slf4j.impl.StaticLoggerBinder” – Summary
Remember, the absence of SLF4J Binding runs into Failed to load class “org.slf4j.impl.StaticLoggerBinder” Error.
- As of SLF4J version 1.6, in the absence of a binding, SLF4J will default to a no-operation (NOP) logger implementation.
- 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.
5. Download the Eclipse Project
That was an article about how to solve the error
slf4j: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
You can download the full source code of this example here: Solving slf4j: Failed to load class “org.slf4j.impl.StaticLoggerBinder”
Last updated on Feb. 27th, 2022