Sub topic 2
How Spring Boot Logging Is Assembled: SLF4J and Logback
This chapter explains which libraries make up Spring Boot's logging, how they connect, and what goes wrong when they don't. It assumes you can read basic Java, have a Spring Boot project built with Maven or Gradle, and know that application.properties holds configuration. Later chapters cover log levels, writing statements, and configuration in detail. Here the focus is the structure underneath.
Why there are several libraries
Java has had many logging libraries over the years: Log4j, java.util.logging (JUL), Commons Logging, Logback, Log4j2. Libraries you depend on each picked one, so a single application can contain code that logs through several different APIs. If each library wrote to its own destination in its own format, you would have no single place to read or control the output.
The standard solution is to separate two roles:
- A logging facade is an API that your code calls. It does not write anything itself.
- A logging implementation (also called a backend) receives those calls and does the real work: filtering by level, formatting, and writing to the console or a file.
SLF4J (Simple Logging Facade for Java) is the facade used by most of the Java ecosystem. Logback is an implementation written by the same author as SLF4J, and it is the one Spring Boot uses by default.
Your code and your libraries call SLF4J. Logback is chosen once, at the edge, and can be replaced without touching application code.
The default stack
When you add spring-boot-starter-web (or almost any other starter), you get spring-boot-starter-logging as a transitive dependency. That starter pulls in three kinds of artifacts:
| Artifact | Role |
|---|---|
| `logback-classic` (with `logback-core`) | The Logback implementation. It implements the SLF4J API directly. |
| `log4j-to-slf4j` | A bridge that redirects calls made to the Log4j2 API into SLF4J. |
| `jul-to-slf4j` | A bridge that redirects `java.util.logging` records into SLF4J. |
Spring itself logs through Apache Commons Logging, which Spring Boot redirects into SLF4J (through the spring-jcl module). The result is that every logging call in the JVM ends up in Logback, whichever API produced it.
To see what your build actually contains, ask the build tool:
./mvnw dependency:tree -Dincludes=org.slf4j,ch.qos.logback,org.apache.logging.log4jFor Gradle, use ./gradlew dependencies --configuration runtimeClasspath and look for the same names.
What Spring Boot adds on top of Logback
Logback on its own looks for logback.xml and, without it, logs everything at DEBUG to the console. Spring Boot wraps it with a LoggingSystem abstraction. During startup, before the application context exists, Spring Boot's logging system initializes Logback with defaults it supplies. Those defaults include:
- A console appender with a colored, readable pattern.
- A root level of
INFO. - Property-driven configuration, such as
logging.level.*andlogging.file.name, which you set inapplication.propertiesorapplication.yml. - Support for a
logback-spring.xmlfile, which can use Spring-specific features such as profiles.
Because of this wrapper, most applications never write Logback XML. You set properties and Spring Boot translates them into Logback configuration. The same property names work if you later switch the backend, because LoggingSystem has implementations for Logback, Log4j2, and JUL.
Initialization order
The order matters because logging has to work before Spring has read any configuration.
- The JVM starts and
SpringApplicationbegins running. - A logging system is detected from the classpath and initialized with safe defaults, so early startup messages are not lost.
- The environment is loaded (properties files, environment variables, command-line arguments).
- Logging is reinitialized using the environment's
logging.*properties and anylogback-spring.xml.
A consequence is that a plain logback.xml is read in step 2, before Spring can process it. That is why it cannot use <springProfile> or Spring properties. The -spring variant is deferred to step 4. This difference is covered in the chapter on logback-spring.xml.
Getting a logger
Application code depends only on the SLF4J API. You create a Logger per class, usually as a private static final field:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class GreetingService {
private static final Logger log = LoggerFactory.getLogger(GreetingService.class);
public String greet(String name) {
log.info("Greeting {}", name);
return "Hello, " + name;
}
}LoggerFactory.getLogger(GreetingService.class) returns a logger named after the fully qualified class name, for example com.example.demo.GreetingService. Logger names form a hierarchy separated by dots. A logger named com.example.demo.GreetingService is a child of com.example.demo, which is a child of com.example, and so on up to the root logger. A child inherits its level from the nearest ancestor that has one configured. This hierarchy is what makes it possible to set a level for a whole package with a single property.
If you use Lombok, @Slf4j generates an equivalent field named log. It still produces an SLF4J logger, so everything in this chapter applies unchanged.
Notice that the import is org.slf4j.Logger. Importing ch.qos.logback.classic.Logger would compile, but it ties your code to the backend and defeats the point of the facade.
Reading the default output
A default Spring Boot log line looks like this:
2026-12-01T10:52:45.206Z INFO 4321 --- [demo] [ main] c.e.demo.GreetingService : Greeting AnaFrom left to right, the fields are: timestamp, level, process ID, a --- separator, the application name (shown when spring.application.name is set, in recent Boot versions), the thread name, the abbreviated logger name, and the message. The exact layout differs slightly between Boot versions. Chapter on log patterns shows how to change it.
Common errors with the stack
Most logging problems in Spring Boot come from two causes: more than one backend on the classpath, or a bridge that loops back on itself.
Multiple SLF4J bindings
If you add spring-boot-starter-log4j2 without excluding spring-boot-starter-logging, both Logback and Log4j2 are present. Spring Boot usually detects this and picks one, but you may see a startup warning such as:
SLF4J: Class path contains multiple SLF4J providers.
SLF4J: Found provider [ch.qos.logback.classic.spi.LogbackServiceProvider@1a2b3c]
SLF4J: Found provider [org.apache.logging.slf4j.SLF4JServiceProvider@4d5e6f]SLF4J 2.x selects one provider and ignores the rest, but which one wins is not something to depend on. The fix is to exclude the starter you do not want, which the Log4j2 chapter shows.
Bridge loops
A bridge sends one API into another. If both directions are present, a message can bounce forever. For example, log4j-over-slf4j (redirects Log4j 1.x into SLF4J) together with slf4j-log4j12 (sends SLF4J into Log4j 1.x) causes a StackOverflowError at startup. Spring Boot's default starter avoids this. It happens when a third-party dependency drags in an old binding. Use the dependency tree command above and exclude the offending artifact.
No output at all
If a logging library you added brings an SLF4J API without any provider, SLF4J prints a notice that it is using a no-operation implementation and discards every message:
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementationIn a Spring Boot application this usually means spring-boot-starter-logging was excluded without adding a replacement.
Version mismatch
Spring Boot 3 uses SLF4J 2.x. Overriding the SLF4J or Logback version in your build to something much older can break the provider lookup. Prefer the versions managed by the Spring Boot dependency management, and change them only for a specific reason such as a security fix.
Checking which backend is active
You can confirm the backend at runtime. This small runner prints the logger factory class that SLF4J selected:
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class LoggingBackendReport implements CommandLineRunner {
@Override
public void run(String... args) {
System.out.println("SLF4J factory: " + LoggerFactory.getILoggerFactory().getClass().getName());
}
}With the default stack it prints ch.qos.logback.classic.LoggerContext. With Log4j2 it prints org.apache.logging.slf4j.Log4jLoggerFactory.
Decision criteria
For most projects the right choice is to keep the defaults. Consider a change only when you have a concrete reason.
- Keep Logback if you have no special needs. It is well tested with Spring Boot, supports profile-aware configuration, and has built-in rolling and structured output.
- Consider Log4j2 if your organization standardizes on it, or if you need its asynchronous logging for very high throughput. The chapter on switching backends covers the steps.
- Always call SLF4J from application code, even if you change the backend. This keeps the switch cheap.
Exercise
Your application starts and prints "SLF4J: Defaulting to no-operation (NOP) logger implementation", and no log lines appear. You recently excluded spring-boot-starter-logging from spring-boot-starter-web. What is the cause, and what are two ways to fix it?
Show answer
Excluding the logging starter removed Logback, the only SLF4J provider on the classpath, so SLF4J falls back to a no-op logger that discards messages. Fix it by removing the exclusion so the default Logback starter returns, or by adding a replacement such as spring-boot-starter-log4j2 so that a provider is present.
Exercise
Why can a class named com.example.orders.OrderService be controlled by a level set on com.example.orders, and which level applies if neither is configured?
Show answer
Logger names form a dot-separated hierarchy, and a logger inherits the level of its nearest configured ancestor. If neither com.example.orders nor any parent up to com.example is configured, the logger inherits from the root logger, whose level in Spring Boot defaults to INFO.