48

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

  • log4j-api-2.3.jar
  • log4j-core-2.3.jar
  • log4j-sl4j-impl-2.3.jar
  • log4j-to-sl4j-2.3.jar
  • slf4j-api-1.7.12.jar

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?

Eugene Gr. Philippov
  • 1,908
  • 2
  • 23
  • 18
Akshay
  • 2,622
  • 1
  • 38
  • 71

3 Answers3

80

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.

Daniel Hári
  • 7,254
  • 5
  • 39
  • 54
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 9
    You can remove the log4j-to-sl4j in Gradle: configurations { all*.exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j' } – dmotta Jun 05 '18 at 18:45
  • my case is removing the upper one, guess either is okay. – zinking Mar 02 '21 at 09:13
3

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

LottaLava
  • 889
  • 1
  • 9
  • 21
1

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'
}