Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

SLF4J: Failed to load class org.slf4j.impl.StaticLoggerBinder

farenda 2016-08-03 1

SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder” is an error that people face very often. Here we show what is the reason and how to fix it!

Failing Java project

SLF4J-API is only an API

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.21</version>
</dependency>

It means that the following JAR contains only interfaces (e.g. Logger) and helper classes, but it is not a logging framework like log4j. It’s just a facade!

Java code that calls SLF4J-API

package com.farenda.loggers;

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

public class LoggingWithSlf4j {

    private static final Logger log
            = LoggerFactory.getLogger(LoggingWithSlf4j.class);

    public static void main(String[] args) {
        log.info("Hello Slf4j!");
    }
}

The error message when no logging framework present

The above code produces the following output:

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

SLF4J haven’t detected any logging framework so it switched to the only API implementation it has – NOP – that does nothing. :-)

Binding SLF4J to a logging framework

To fix that we need to add a logging framework to our project configuration:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.21</version>
    <scope>runtime</scope>
</dependency>

Now, SLF4J will detect at runtime that there is a logging framework on a classpath and will use it for logging. Notice that we’ve declared slf4j-simple in runtime scope, because it is not needed for compilation – our code is using the facade.

Running the same code prints to logs as expected:

[main] INFO com.farenda.loggers.LoggingWithSlf4j - Hello Slf4j!

Logging frameworks bindings

  • slf4j-log4j12: binding for log4j version 1.2
  • slf4j-jdk14: binding for java.util.logging
  • slf4j-nop: binding for NOP (No OPeration)
  • slf4j-simple: binding that prints to System.err
  • slf4j-jcl: binding for Jakarta Commons Logging
  • logback-classic: native implementation.
Share with the World!
Categories Java Tags java, logging, slf4j
Previous: Java Reverse String in 2 ways
Next: SLF4J: Class path contains multiple SLF4J bindings

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored