Video in progress
What is SLF4J and how is it connected to Logback?
SLF4J is a common interface (a facade) for writing logs, and Logback is the real logging library that implements that interface. The code only knows SLF4J, and the implementation underneath can be swapped.
📖 Answer
SLF4J (Simple Logging Facade for Java) doesn't write logs itself. It provides the Logger and LoggerFactory interfaces. Writing the log to a file or the console is the job of a binding: Logback, Log4j2 and so on.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PaymentService {
private static final Logger log = LoggerFactory.getLogger(PaymentService.class);
}
Why do you need this separation? A library you write will be used by another team. If the library is tied directly to Log4j2, they are forced to take Log4j2 too. With SLF4J, everyone picks their own implementation.
Spring Boot uses SLF4J + Logback by default (spring-boot-starter-logging). If you need Log4j2, you add spring-boot-starter-log4j2 and exclude the logging starter. Lombok's @Slf4j annotation also generates this Logger field for you.
Sign up to mark studied questions and save your progress.