SLF4J Binding to Simple Logger Example


How to use SLF4J API with SLF4J Simple Logger?

✍: Guest


If you want to use SLF4J API with SLF4J Simple Logger, you need specify slf4j-api-*.jar and slf4j-simple-*.jar in Java classpath 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 and slf4j-simple-1.7.31.jar.

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

$ java -cp slf4j-api-1.7.31.jar:slf4j-simple-1.7.31.jar

Log class: org.slf4j.impl.SimpleLogger

[main] ERROR fyiLog - Hello - error
[main] WARN fyiLog - Hello - warn
[main] INFO fyiLog - Hello - info

3. Review the output:

  • The SLF4J API finds the SLF4J Simple Logger library in the classpath.
  • The SLF4J API binds the SLF4J Simple Logger library automatically.
  • The SLF4J Simple Logger is implemented in org.slf4j.impl.SimpleLogger class.
  • SLF4J Simple Logger prints logging messages to the console.
  • SLF4J Simple Logger filters out and not prints DEBUG and TRACE messages.


SLF4J Binding to JDK Loger Example

SLF4J No Binding Example

Using slf4j-*.jar in Java Programs

⇑⇑ SLF4J - Simple Logging Facade for Java

2021-12-23, 394👍, 0💬