25

I have the following dependencies in my build.gradle file.

compile 'org.slf4j:slf4j-api:1.7.25'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.1'

On running my unit tests, the following logs are displayed.

exclude patterns:SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in 
[jar:file:....gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-slf4j-impl/2.7/382b070836b8940a02d28c936974db95e9bfc3a4/log4j-slf4j-impl-2.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/z002qz1/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-slf4j-impl/2.9.1/a97a849b18b3798c4af1a2ca5b10c66cef17e3a/log4j-slf4j-impl-2.9.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
java.lang.NoClassDefFoundError: org/apache/logging/log4j/util/ReflectionUtil
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)

I'm using springboot 2.0.4.RELEASE. I'm hoping this is just some sort of version mismatch issue. Any insights are appreciated.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
hisdudeness
  • 438
  • 1
  • 5
  • 15
  • 1
    Check this, https://stackoverflow.com/questions/22896243/maven-slf4j-class-path-contains-multiple-slf4j-bindings/22896270 – Habil Oct 08 '18 at 11:03

2 Answers2

28

The error: java.lang.NoClassDefFoundError: org/apache/logging/log4j/util/ReflectionUtil

is because log4j2 since version 2.9.0 has been removed that class from api jar (log4j-api-2.x.x.jar).

The last version that has it, is 2.8.2

Probably you have mixed versions in the classpath.

devwebcl
  • 2,866
  • 3
  • 27
  • 46
  • Hello devwebcl, what is the way to use the most recent version of Log4j then? I didn't get the reason of mixed versions. – learner Apr 03 '19 at 11:40
  • 2
    You need to have only one version of log4j2 in your classpath, you cannot have 2.8.2 (or lower) and 2.9.0+ in the same classpath. Otherwise you'll probably hit that NoClassDefFoundError. Therefore, try using only the last version of log4j2: 2.11.2 – devwebcl Apr 03 '19 at 12:47
  • Thanks! This solved my issue where I had a too recent version of log4j in a soapUI project. It worked when I downgraded to 2.8.2! – Arizon Nov 17 '22 at 13:02
5

Correct way to configure log4j2 in spring boot is like this:

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-log4j2'
}

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

It's explained in the documentation.

Strelok
  • 50,229
  • 9
  • 102
  • 115