Here’s a detailed breakdown of SLF4J, Lombok, and how they compare to Log4J, along with their configurations and usage.


1. SLF4J (Simple Logging Facade for Java)

SLF4J is a logging framework that acts as an abstraction layer for various logging implementations. It allows you to plug in different logging frameworks (like Log4J, Logback, or java.util.logging) without changing your application code.

Why use SLF4J?

SLF4J vs. Log4J vs. Lombok

Feature SLF4J Log4J Lombok
Type Logging Facade (Interface) Logging Implementation Annotation-based Code Generator
Purpose Provides a unified logging API Full-fledged logging framework Reduces boilerplate code
Dependency Requires an actual logging implementation (e.g., Logback, Log4J) Self-contained Works with SLF4J/Log4J
Performance Efficient parameterized logging Slightly more overhead Enhances logging with annotations

2. SLF4J - Environment Setup

Step 1: Add SLF4J and Logback Dependencies (Maven)

<dependencies>
    <!-- SLF4J API -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>2.0.7</version>
    </dependency>

    <!-- Logback as SLF4J Implementation -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.4.5</version>
    </dependency>
</dependencies>


3. Sample Logging using SLF4J

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

public class SLF4JExample {
    private static final Logger logger = LoggerFactory.getLogger(SLF4JExample.class);

    public static void main(String[] args) {
        logger.info("This is an info message");
        logger.warn("This is a warning message");
        logger.error("This is an error message");
    }
}


4. SLF4J - Error Messages, Warning Levels, Parameterized Logging

Logging Levels