Akshay
Akshay

Reputation: 2763

Using log4j2 with slf4j: java.lang.StackOverflowError

So I have tried following this (non-maven implementation) and requirements in their web site for adding slf4j to log4j. and tried using this code

public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger(Main.class);
        logger.info("test");
    }

and added the following to my library

when I try running it I get the following error

Exception in thread "main" java.lang.StackOverflowError
    at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
    at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:964)
    at org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:40)
    at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:37)
    at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:29)
    at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:284)
    at org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:41)
    at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:37)
    at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:29)
    at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:284)...

Any idea where I am going wrong?

Upvotes: 48

Views: 41274

Answers (4)

A4L
A4L

Reputation: 17595

In case someone's search for java.lang.StackOverflowError at org.slf4j.jul.JDK14 led to this question.

The root cause is already explained in Andreas's answer.

In my case it was the mockserver dependency

<dependency>
   <groupId>org.mock-server</groupId>
   <artifactId>mockserver-netty-no-dependencies</artifactId>
   <version>5.15.0</version>
</dependency>

Which had the classes from slf4j/jul bundled inside it, which resulted in the following stacktrace:

java.lang.StackOverflowError
    at org.slf4j.jul.JDK14LoggerAdapter.log(JDK14LoggerAdapter.java:167)
    at org.slf4j.bridge.SLF4JBridgeHandler.callLocationAwareLogger(SLF4JBridgeHandler.java:221)
    at org.slf4j.bridge.SLF4JBridgeHandler.publish(SLF4JBridgeHandler.java:303)
    at java.logging/java.util.logging.Logger.log(Logger.java:979)
    at org.slf4j.jul.JDK14LoggerAdapter.innerNormalizedLoggingCallHandler(JDK14LoggerAdapter.java:156)
    at org.slf4j.jul.JDK14LoggerAdapter.log(JDK14LoggerAdapter.java:172)
    at org.slf4j.bridge.SLF4JBridgeHandler.callLocationAwareLogger(SLF4JBridgeHandler.java:221)
    at org.slf4j.bridge.SLF4JBridgeHandler.publish(SLF4JBridgeHandler.java:303)
    at java.logging/java.util.logging.Logger.log(Logger.java:979)
    at org.slf4j.jul.JDK14LoggerAdapter.innerNormalizedLoggingCallHandler(JDK14LoggerAdapter.java:156)
    at org.slf4j.jul.JDK14LoggerAdapter.log(JDK14LoggerAdapter.java:172)
    at org.slf4j.bridge.SLF4JBridgeHandler.callLocationAwareLogger(SLF4JBridgeHandler.java:221)

As the those classes were not inside a transitive dependency an exclusion was not possible. So the solution was just to replace the mentioned dependency with all denepndies required to run mockserver, again in my case

<dependency>
  <groupId>org.mock-server</groupId>
  <artifactId>mockserver-netty</artifactId>
   <version>5.15.0</version>
</dependency>

Upvotes: 0

Darshan Kumar
Darshan Kumar

Reputation: 11

Idea 1:

The above answer works. Mine was a multi module project and the log4j-to-slf4j-2.3.jar dependency was being by pulled by a different project. I had to scan each project to figure which project was pulling in this. I used the following command to do at a project level.

gradlew common:dependencies --configuration runtime

common is the name of the project configuration runtime - you can change this to suit your need


Idea 2:

If you are using spring which has multi module projects. Then you can simply add the following config in every subproject's build.gradle or dependency.gradle

configurations {
    all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

Upvotes: 1

LottaLava
LottaLava

Reputation: 889

Just in case, if you have logback in your classpath, it must be removed as well!

Upvotes: 3

Andreas
Andreas

Reputation: 159086

You're creating a call-loop with log4j-slf4j-impl-2.3.jar and log4j-to-slf4j-2.3.jar.

log4j-slf4j-impl-2.3.jar is the implementation of the adapter that sends slf4j calls to log4j.

log4j-to-slf4j-2.3.jar is sending log4j calls right back to slf4j. Remove this one.

Upvotes: 80

Related Questions