Sub topic 2
How Spring Boot logging is assembled: SLF4J and Logback
This chapter assumes you can read basic Java, have a Spring Boot application built with Maven or Gradle, and know where application.properties lives. It explains which libraries take part in logging and how they connect. Later chapters cover levels, configuration, and output.
Facade and implementation
Java has several logging libraries, and different libraries in your dependency tree may each pick a different one. Spring Boot handles this by separating two roles:
- A facade is the API your code calls. It does not write anything itself. SLF4J (Simple Logging Facade for Java) is the facade.
- A backend is the library that formats messages and writes them to the console or a file. Logback is the default backend in Spring Boot.
Your code depends only on the facade. You can change the backend later without editing a single log statement.
What the starter gives you
Every spring-boot-starter-* depends on spring-boot-starter-logging. You never declare it yourself. It brings these pieces:
| Piece | Role |
|---|---|
| `slf4j-api` | The `Logger` API that your code uses |
| `logback-classic` | The backend that does the work |
| `jul-to-slf4j` | Redirects `java.util.logging` calls into SLF4J |
| `log4j-to-slf4j` | Redirects Log4j 2 API calls into SLF4J |
The two bridges matter because many third-party libraries log with their own API. Without a bridge, their messages would bypass your configuration and appear in a different format. With the bridges, everything ends up in one place.
Getting a logger
You ask SLF4J for a Logger by passing a class. The logger takes its name from that class, and later chapters use these names to set levels per package.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class OrderService {
private static final Logger log = LoggerFactory.getLogger(OrderService.class);
public void placeOrder(String orderId) {
log.info("Placing order {}", orderId);
}
}Note the imports. They come from org.slf4j, not from ch.qos.logback. If you import a Logback class in application code, you tie yourself to the backend and lose the benefit of the facade.
What Spring Boot adds
Spring Boot configures Logback before your application context starts, so startup messages are already formatted. Without any configuration file, you get:
- Console output at
INFOlevel. - A default pattern with timestamp, level, process ID, thread, logger name, and message.
- Control through
logging.*properties, which Boot translates into Logback settings.
You only write a Logback XML file when properties are not enough. Boot looks for logback-spring.xml on the classpath. Use that name rather than plain logback.xml. Logback reads logback.xml itself, before Spring can process it, and that blocks Spring-specific features such as profile sections.
Common errors
- Multiple SLF4J bindings. A dependency that brings its own backend, such as
slf4j-simple, puts two backends on the classpath. SLF4J warns at startup and picks one. Runmvn dependency:treeorgradle dependenciesand exclude the extra one. - Logs in two formats. Some library logs bypass the bridges, usually because the bridge is excluded or the library uses a different API such as Commons Logging without its bridge. Check that
spring-boot-starter-loggingis still present. - Importing the wrong
Logger. IDEs sometimes suggestjava.util.logging.Logger. Useorg.slf4j.Logger.
Exercise
Your application code imports only org.slf4j.Logger, yet a third-party library that logs through java.util.logging shows up in your Logback console output. Which part of the starter makes this happen, and why does it matter?
Show answer
The jul-to-slf4j bridge. It forwards java.util.logging calls into SLF4J, which hands them to Logback. Because of this, library messages follow the same pattern, levels, and output destinations as your own logs, so you configure logging in one place.