SLF4J No Binding Example


What happens if I use slf4j-api-*.jar without any logging library to bind with?

✍: Guest


If you use slf4j-api-*.jar only, you will get a warning message. And all logging messages will be dropped as shown in this tutorial.

1. Write a simple Java program,, to use SLF4J API:

// Copyright (c)
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Hello {
  private static final Logger log = LoggerFactory.getLogger("fyiLog");
  public static void main(String[] args) {
    System.out.println("Log class: "+log.getClass().getName());
    log.error("Hello - error");
    log.warn("Hello - warn");"Hello - info");
    log.debug("Hello - debug");
    log.trace("Hello - trace");

2. Run with slf4j-api-1.7.31.jar only.

$ java -version 
java version "15" 2020-09-15

$ java -cp slf4j-api-1.7.31.jar

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

Log class: org.slf4j.helpers.NOPLogger

3. Review the output:

  • The SLF4J API finds no logging library to bind with. So it uses the built-in no-operation (NOP) logger.
  • The built-in no-operation (NOP) logger is implemented in org.slf4j.helpers.NOPLogger class.
  • All logging messages are dropped.


SLF4J Binding to Simple Logger Example

Using slf4j-*.jar in Java Programs

Using slf4j-*.jar in Java Programs

⇑⇑ SLF4J - Simple Logging Facade for Java

2021-12-23, 852🔥, 0💬