1

Trying to solve the infamous SLF4J multiple bindings issue on Gradle. There are about a million solutions for Maven on here but none of them are translating to Gradle (not a Gradle expert obviously). I've tried a handful of solutions involving configurations however none have worked correctly. Here is there error I'm getting (as I mentioned I'm trying to suppress the warnings)

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in     [jar:file:~/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-nop/1.7.22/3a4392836f875995446373b008e39cdb9a532fbe/slf4j-nop-1.7.22.jar!/org/slf4j/impl/StaticLoggerBinder.class]
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: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.helpers.NOPLoggerFactory]

This is the only SLF4J dependency in my project:

dependencies {
    compile group: 'org.slf4j', name: 'slf4j-nop', version: '1.7.+'
    ...
}

Also, I'm running into the issue when I run my unit tests if that helps.

UPDATE: Figured out one of the dependencies is using org.apache.logging which in turn uses SLF4J. If I exclude the group for the apache logger the entire program fails because that dependency throws a ClassNotFoundException. So now what?

MarkII
  • 872
  • 1
  • 9
  • 26
  • What other dependencies are you including with Gradle, besides SLF4J? – badjr Jan 24 '17 at 05:16
  • A long list of them, albeit none are SLF4J or LOG4J, directly at least. Obviously one of them or multiple have it. – MarkII Jan 24 '17 at 15:21
  • Have you tried `runtime.exclude group`, as shown in [this answer](http://stackoverflow.com/a/21765656/1461484)? – badjr Jan 24 '17 at 15:29
  • Can try it now, I've done many variations of exclude in the configurations and none seem to do anything. Oddly enough I've removed everything and the logging turned off on its own. – MarkII Jan 24 '17 at 15:39
  • Yeah it's not logging anything anymore. Not sure what I should do with this question but I have no additional changes to my Gradle file and it just stopped :/ – MarkII Jan 24 '17 at 15:42
  • I don't have any other ideas. For the question, one option would be to add a bounty. – badjr Jan 24 '17 at 15:47
  • Think I found the culprit, adding an update. – MarkII Jan 25 '17 at 18:02

4 Answers4

5

Was able to solve through a combination of the comments above. After finding the dependencies that had SLF4J I added the exclusion based on what @badjir mentioned. That solved the main issue at hand although another one has arisen with LOG4J, which if proceeds I'll ask another question. Example of one of the dependencies:

compile (group: 'com.sparkjava', name: 'spark-core', version: '2.5.4') {
    exclude group: 'org.slf4j'
}
MarkII
  • 872
  • 1
  • 9
  • 26
  • This is what I told you to do :) – Sage Smith Jan 26 '17 at 07:59
  • Yes sorry, tried to attribute both of the answer providers. Although your exact solution didn't work right either, ended up following the Gradle Docs example like the other post provided. So in a sense you were both mostly right. – MarkII Jan 27 '17 at 16:54
1

You can exclude the dependency that you do not wish to use. The Gradle docs says you can exclude a dependency like

exclude group: 'org.unwanted', module: 'iAmBuggy' //by both name and group

badjr
  • 2,166
  • 3
  • 20
  • 31
  • That looks like it works only if I know the dependency that is causing this problem. I'm trying to exclude for any and all dependencies. – MarkII Jan 23 '17 at 21:46
  • Not sure what happened but it randomly stopped logging... No changes to my Gradle file. This is frustrating. – MarkII Jan 24 '17 at 00:00
  • @MarkII, it tells you which jar files the conflicting dependencies are occuring though. Is there a way you can look at all the dependencies of your project and see which ones are including those jar files? – badjr Jan 24 '17 at 01:16
  • Possible but that would be pretty painstaking to go through each dependency and research its dependencies and so on. From how easy it seemed to do in Maven I figured there was a way to exclude for the entire project. – MarkII Jan 24 '17 at 15:23
1

This error is caused because you have multiple libraries that use the slf4j logging framework. What you will need to do is locate the library that is transitively bringing in slf4j as a dependency and add an exclude (group: "slf4j's-group"). An easy way to locate which package is causing the issue is to use gradle dependencies which will produce a tree of all of the dependencies.

Sage Smith
  • 441
  • 3
  • 10
  • Will give this a try today since the warnings have returned – MarkII Jan 25 '17 at 17:40
  • Ran the dependencies and found 3 that had slf4j in some form, added your exclusion (no luck) and added the exclusion based off what I thought from the Gradle docs (no luck). Honestly really hate SLF4J at this point. – MarkII Jan 25 '17 at 17:55
0

In my case I had 3 bindings (logback-classic, slf4j-log4j12, and log4j-nop) and do not have the time to comb through dependencies. I read this http://buransky.com/programming/exclude-log4j-to-use-slf4j-with-logback-in-a-gradle-project/ and added these to build.gradle and it worked.

configurations.all {
  exclude group: "org.slf4j", module: "slf4j-nop"
  exclude group: "org.slf4j", module: "slf4j-log4j12"
}
RudyD
  • 667
  • 7
  • 11