2

I have a unit test and the only thing I get is a stack overflow as follows:

java.lang.StackOverflowError
    at org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:125)
    at org.apache.logging.log4j.util.StackLocatorUtil.getCallerClass(StackLocatorUtil.java:55)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:42)
    at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
    at org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:39)
    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:52)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
... many more

I am tring to use log4j and slf4j. I have excluded logback if I dont i get a diferent error about casting log4j context to slf4j context.

I have cut down the build.gradle file to the minimum dependacies:

testCompile ('org.springframework.boot:spring-boot-starter-test'){
    exclude group: 'ch.qos.logback', module: 'logback-classic'
}
testCompile 'com.fedex.gss.extras:gss-extras-base'
testCompile 'org.mockito:mockito-core'
testCompile('junit:junit')

plus importing the BOM for log4j and springboot.

Any ideas?

Many thanks.

Arnaud
  • 17,229
  • 3
  • 31
  • 44
Ian
  • 412
  • 1
  • 4
  • 18
  • Can we see your code so that a suitable solution can be proposed please – jr593 Feb 28 '19 at 08:55
  • 1
    These may be related : https://www.slf4j.org/codes.html (search for "StackOverflowError") and https://stackoverflow.com/questions/32366586/using-log4j2-with-slf4j – Arnaud Feb 28 '19 at 08:57
  • 1
    As @Arnaud mentioned, these are usually caused by having both `log4j-over-slf4j` as `slf4j-log4j`. The first one makes sure that all direct access to log4j goes through slf4j in stead, while the latter is the briding from slf4j to log4j. If you combine both, you're in an endless loop between calls from slf4j to log4j and vice versa. With `gradle dependencies` you should be able to find out where the other dependency comes from, and being able to exclude it. – g00glen00b Feb 28 '19 at 10:27
  • Thanks, eventually worked it out. Solution was to add the following excludes to the spring-boot-starters: `exclude group: 'ch.qos.logback', module: 'logback-classic' exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'` Then add an additional dependacy `compile 'org.slf4j:slf4j-jdk14'` Thanks to you all. – Ian Mar 01 '19 at 13:37

0 Answers0