Quick revision
The short answer
The StackOverflowError comes from a loop between two adapters that point in opposite directions for the same logging API: one sends that API's calls into SLF4J, and the other sends SLF4J's calls back into the same API, so every log event goes round and round until the stack runs out. The real loop pairs are jcl-over-slf4j with slf4j-jcl, log4j-over-slf4j with slf4j-log4j12 or slf4j-reload4j, and log4j-to-slf4j with log4j-slf4j-impl or log4j-slf4j2-impl. We find which dependency brought each jar with mvn dependency:tree or gradle dependencyInsight, pick one backend, keep only bridges that point towards it, exclude the opposite adapters, and add an enforcer rule so the pair cannot return.
What to remember
In this answer, we separated bridges, which send another API's calls into SLF4J, from adapters, which send SLF4J's calls to a backend, and saw that a bridge and an adapter for the same API form an infinite loop that ends in StackOverflowError. We corrected a common confusion: log4j-over-slf4j loops with slf4j-log4j12 or slf4j-reload4j, while log4j-slf4j-impl loops with log4j-to-slf4j, the pair Spring Boot users usually meet. We traced the unwanted jar with dependency:tree or dependencyInsight, kept one backend with arrows only towards it, and banned the opposite adapters in the build.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The StackOverflowError comes from a loop between two adapters that point in opposite directions for the same logging API: one sends that API's calls into SLF4J, and the other sends SLF4J's calls back into the same API, so every log event goes round and round until the stack runs out. The real loop pairs are jcl-over-slf4j with slf4j-jcl, log4j-over-slf4j with slf4j-log4j12 or slf4j-reload4j, and log4j-to-slf4j with log4j-slf4j-impl or log4j-slf4j2-impl. We find which dependency brought each jar with mvn dependency:tree or gradle dependencyInsight, pick one backend, keep only bridges that point towards it, exclude the opposite adapters, and add an enforcer rule so the pair cannot return.
Why this question is asked
This failure is dramatic, a crash at startup with thousands of identical stack frames, but its cause is a single unwanted jar, usually pulled in by some unrelated dependency. Interviewers ask it because solving it needs a clear picture of which jars send logs in which direction, and the ability to trace a dependency to its origin. In this answer, we will recap the two kinds of jars around SLF4J, see exactly how the loop forms, correct a common confusion about which pairs actually loop, find the jar's origin, fix the dependencies for a Logback application, and prevent the problem from coming back.
1A Quick Recap: Bridges and Adapters
Around SLF4J there are two kinds of jars, and their names are confusingly similar. A bridge takes calls made to some other logging API and sends them into SLF4J. jcl-over-slf4j does this for Commons Logging, log4j-over-slf4j for the Log4j 1 API, log4j-to-slf4j for the Log4j2 API and jul-to-slf4j for java.util.logging. An adapter, also called a binding or provider, goes the other way: it takes SLF4J calls and sends them to a backend. logback-classic sends them to Logback, slf4j-jcl to Commons Logging, slf4j-log4j12 or slf4j-reload4j to Log4j 1, and log4j-slf4j-impl or log4j-slf4j2-impl to Log4j2.
A healthy application has many bridges, all pointing into SLF4J, and exactly one adapter pointing out of it to the chosen backend.
2How the Loop Forms
Now put jcl-over-slf4j and slf4j-jcl on the same classpath. A library calls the Commons Logging API. jcl-over-slf4j has replaced the Commons Logging classes, so the call goes into SLF4J. SLF4J's adapter is slf4j-jcl, so it sends the event to Commons Logging. Commons Logging is again jcl-over-slf4j, which sends it to SLF4J, and so on. Each round adds frames to the stack until the JVM throws StackOverflowError.
6 messages in order. Play them, or step through them one at a time.
A bridge and an adapter for the same API, pointing in opposite directions.
3Which Pairs Actually Loop
The question mentions log4j-over-slf4j together with log4j-slf4j-impl, and this pairing needs care, because the names mislead. log4j-over-slf4j bridges the old Log4j 1 API into SLF4J, while log4j-slf4j-impl sends SLF4J to Log4j2, a different API. On their own they form a chain, Log4j 1 calls to SLF4J to Log4j2, not a loop. The loop partners are the ones for the same API: log4j-over-slf4j loops with slf4j-log4j12 or slf4j-reload4j, and log4j-slf4j-impl loops with log4j-to-slf4j, which bridges the Log4j2 API back into SLF4J.
In Spring Boot, that second pair is the common one in practice. spring-boot-starter-logging brings logback-classic plus log4j-to-slf4j and jul-to-slf4j. If someone adds spring-boot-starter-log4j2, or a library that depends on log4j-slf4j-impl, without excluding the default starter, the classpath contains both log4j-to-slf4j and log4j-slf4j-impl. Log4j2 detects this pair and fails fast with a LoggingException saying log4j-slf4j-impl cannot be present with log4j-to-slf4j, and with versions that do not detect it, the result is the StackOverflowError. SLF4J itself detects some pairs too, such as jcl-over-slf4j with slf4j-jcl, and prints a message that it is preempting a StackOverflowError (check your versions for the exact behaviour, as some only warn).
| Bridge into SLF4J | Opposite adapter (loops) | API involved |
|---|---|---|
| jcl-over-slf4j | slf4j-jcl | Commons Logging |
| log4j-over-slf4j | slf4j-log4j12, slf4j-reload4j | Log4j 1 |
| log4j-to-slf4j | log4j-slf4j-impl, log4j-slf4j2-impl | Log4j2 API |
| jul-to-slf4j | slf4j-jdk14 | java.util.logging |
4Finding Where Each Jar Comes From
The jar we did not ask for almost always arrives transitively. The dependency tree, filtered to logging groups, shows the path from our project to it.
Notice that the culprit is a vendor library that declared a backend adapter as a normal dependency, which libraries should never do. The Gradle command answers the same question from the other side: it shows every path that leads to one artifact and why that version was selected.
5Fixing It for a Logback Application
First we decide on one backend. For a standard Spring Boot application that is Logback. Then we keep only bridges that point towards SLF4J, jul-to-slf4j, jcl-over-slf4j where needed and log4j-to-slf4j or log4j-over-slf4j, and exclude every adapter that points away from SLF4J to anything other than Logback.
If the team had chosen Log4j2 instead, the fix would be mirrored: exclude spring-boot-starter-logging, which removes logback-classic and log4j-to-slf4j, and add spring-boot-starter-log4j2. Either way, the rule is one backend and arrows only towards it.
6Preventing It From Coming Back
An exclusion fixes one path, but the next dependency upgrade can bring another. A Maven Enforcer bannedDependencies rule in the parent POM, listing the adapters that must never appear with our backend, fails the build the moment any dependency brings one back. For Logback services, it bans slf4j-jcl, slf4j-log4j12, slf4j-reload4j, slf4j-jdk14, slf4j-simple, log4j-slf4j-impl and log4j-slf4j2-impl. Gradle can do the same with a resolution rule or by excluding these modules from all configurations. A startup test that checks the active SLF4J provider adds a second safety net.
7Follow-up questions
How do you route java.util.logging into SLF4J and what does it cost?
Add jul-to-slf4j and install its SLF4JBridgeHandler, which Spring Boot does automatically when Logback is the backend. The cost is that JUL checks levels before calling handlers, so with the root JUL level wide open, every disabled JUL call still creates a LogRecord and passes through the bridge before Logback drops it. Logback's LevelChangePropagator, configured with resetJUL set to true, copies Logback levels into JUL so disabled calls are rejected early on the JUL side.
Why does a library declaring log4j-slf4j-impl cause so much trouble?
Because it chooses the backend for every application that uses it. Libraries should depend only on slf4j-api or the Log4j2 API and leave the adapter to the application. When a vendor library does this, an exclusion and a word with the vendor are the usual answers.
Can both Logback and Log4j2 core be on the classpath safely?
They can coexist as jars, but only one should receive SLF4J events, and the Log4j2 API must be bridged consistently. In practice, having both invites exactly this kind of confusion, so the standard should be one backend per application.
8Conclusion
In this answer, we separated bridges, which send another API's calls into SLF4J, from adapters, which send SLF4J's calls to a backend, and saw that a bridge and an adapter for the same API form an infinite loop that ends in StackOverflowError. We corrected a common confusion: log4j-over-slf4j loops with slf4j-log4j12 or slf4j-reload4j, while log4j-slf4j-impl loops with log4j-to-slf4j, the pair Spring Boot users usually meet. We traced the unwanted jar with dependency:tree or dependencyInsight, kept one backend with arrows only towards it, and banned the opposite adapters in the build.
Open with
A StackOverflowError inside logging almost always means a loop between a bridge and an adapter for the same API. A bridge sends some API's calls into SLF4J, for example jcl-over-slf4j for Commons Logging or log4j-to-slf4j for the Log4j2 API.
Then, if they want more
An adapter sends SLF4J calls out to a backend, like slf4j-jcl or log4j-slf4j-impl. If both exist for the same API, an event goes into SLF4J, back out to that API, and back into SLF4J until the stack overflows.
I would be precise about the pairs: jcl-over-slf4j loops with slf4j-jcl, log4j-over-slf4j loops with slf4j-log4j12 or reload4j, and log4j-to-slf4j loops with log4j-slf4j-impl. log4j-over-slf4j with log4j-slf4j-impl is actually a chain from Log4j 1 to Log4j2, not a loop.
In Spring Boot the common case is the starter-logging bringing log4j-to-slf4j while some library brings log4j-slf4j-impl, and Log4j2 usually fails fast with an exception saying they cannot be present together. To find the origin, I run mvn dependency:tree filtered to org.slf4j and org.apache.logging.log4j, or gradle dependencyInsight on the suspect artifact, which shows which dependency brought it.
Then I choose one backend, Logback by default, keep only bridges that point towards SLF4J, and exclude the opposite adapters. Finally, I add an enforcer bannedDependencies rule for all adapters other than our backend, so the next upgrade cannot bring the loop back.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Spring Boot application fails at startup with StackOverflowError deep inside logging...": https://scrivio-interview-library.netlify.app/java/java-logging/6cc8904643/