1

I'm trying to set up logback in a project that uses gradle as the build tool. This is the build file

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'groovy'
apply plugin: 'maven'


sourceCompatibility = 1.8

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    compile gradleApi()
    compile localGroovy()
    compile 'org.liquibase:liquibase-core:3.0.1'
    compile 'org.jdbi:jdbi:2.71'
    compile 'org.postgresql:postgresql:9.4.1208.jre7'
    compile 'ch.qos.logback:logback-classic:1.1.7'
    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile 'org.powermock:powermock-module-junit4:1.6.1'
    testCompile 'org.powermock:powermock-api-mockito:1.6.1'
}

And this is the logback.groovy file which contains the configuration for logback

import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import org.apache.log4j.FileAppender

root(DEBUG, ["CONSOLE", "FILE"])

appender("FILE", FileAppender) {
    file = "testFile.log"
    append = true
    encoder(PatternLayoutEncoder) {
        pattern = "%level %logger - %msg %n"
    }
}

When I try to log something I was getting this SLF4J warning

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/usr/local/Cellar/gradle/2.12/libexec/lib/gradle-core-2.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/shishir/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.1.7/9865cf6994f9ff13fce0bf93f2054ef6c65bb462/logback-classic-1.1.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.gradle.logging.internal.slf4j.OutputEventListenerBackedLoggerContext]

To solve that I followed the answer to this stack overflow question and added this extra code to my build file

configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    if (details.requested.name == 'logback-classic') {
        details.useTarget 'org.slf4j:slf4j-api:1.7.5'
    }
}

Although this solved the problem of the SLF4J warning, the logging is not working. The logging statements I added are not printed to STDOUT in the pattern specified in logback.groovy. The logs are not being added to the file either. Can someone point me in the right direction?

Community
  • 1
  • 1
Shishir Joshi
  • 337
  • 1
  • 2
  • 15

1 Answers1

0

Gradle comes with its own logging framework that you can't really replace / add to. The best bet is for you to use the Gradle logger in you Gradle plugin.

Ethan
  • 6,883
  • 3
  • 33
  • 41