Exclude log4j to use slf4j with logback in a Gradle project


The goal is to remove log4j from all transitive dependencies in a Gradle project and replace it by slf4j and logback. I had to write this after spending an hour again trying to setup proper logging. Nothing complicated, you only need to know what to do.

Add following to your build.gradle file:

dependencies {
    compile "ch.qos.logback:logback-classic:1.1.3"
    compile "org.slf4j:log4j-over-slf4j:1.7.13"
}

configurations.all {
    exclude group: "org.slf4j", module: "slf4j-log4j12"
    exclude group: "log4j", module: "log4j"
}

That’s it. If it still doesn’t work as expected, enable debugging in logback.xml and dig deeper. Good luck.