I recently upgraded my Spring Boot app from 1.5.10 to 2.0.3 and am now facing this issue: when I add logging.level.root=DEBUG
to my application.properties
, I get the following exception:
log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "background-preinit" java.lang.StackOverflowError
at org.slf4j.bridge.SLF4JBridgeHandler.getSLF4JLogger(SLF4JBridgeHandler.java:198)
at org.slf4j.bridge.SLF4JBridgeHandler.publish(SLF4JBridgeHandler.java:293)
at java.util.logging.Logger.log(Logger.java:738)
at java.util.logging.Logger.doLog(Logger.java:765)
at java.util.logging.Logger.logp(Logger.java:931)
at org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:180)
at org.apache.juli.logging.DirectJDKLog.debug(DirectJDKLog.java:103)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1154)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
at org.slf4j.bridge.SLF4JBridgeHandler.getSLF4JLogger(SLF4JBridgeHandler.java:198)
at org.slf4j.bridge.SLF4JBridgeHandler.publish(SLF4JBridgeHandler.java:293)
at java.util.logging.Logger.log(Logger.java:738)
at java.util.logging.Logger.doLog(Logger.java:765)
at java.util.logging.Logger.logp(Logger.java:931)
at org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:180)
at org.apache.juli.logging.DirectJDKLog.debug(DirectJDKLog.java:103)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1154)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
at org.slf4j.bridge.SLF4JBridgeHandler.getSLF4JLogger(SLF4JBridgeHandler.java:198)
at org.slf4j.bridge.SLF4JBridgeHandler.publish(SLF4JBridgeHandler.java:293)
...
This is only a small portion, as this set of the stack trace repeats over 100 times, causing the StackOverflowError
.
Debugging steps I've tried:
- If I change the logging level to anything else, like
INFO
, I don't get the exception. - I've added a specific dependency for
SLF4JBridgeHandler
in mypom.xml
. - I've made sure that
SLF4JBridgeHandler
is on the classpath of my web app.
What could be causing this issue?
I'll just quote the official slf4j documentation,
jul-to-slf4j.jar and slf4j-jdk14.jar cannot be present simultaneously
The presence of
slf4j-jdk14.jar
, that is thejul
binding forSLF4J
, will forceSLF4J
calls to be delegated tojul
. On the other hand, the presence ofjul-to-slf4j.jar
, plus the installation ofSLF4JBridgeHandler
, by invoking "SLF4JBridgeHandler.install()
" will routejul
records toSLF4J
. Thus, if both jar are present simultaneously (andSLF4JBridgeHandler
is installed),slf4j
calls will be delegated tojul
andjul
records will be routed toSLF4J
, resulting in an endless loop.
Which is what is happening to your case.
Try doing
mvn dependency:tree
and exclude the conflicting dependencies.
In my opinion, spring-boot
is good at managing dependencies like this. So its good to keep the pom.xml
clean with less manually added dependencies.