2016-04-15 117 views
0

我試圖在使用gradle作爲構建工具的項目中設置logback。這是構建文件無法加載logback配置,同時使用帶gradle的經典logback

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' 
} 

這是logback.groovy文件,其中包含的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" 
    } 
} 

當我嘗試登錄的東西我得到這個SLF4J警告

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] 

爲了解決這個問題,我遵循了這個stack overflow question的答案,並將這個額外的代碼添加到我的構建文件中

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

雖然這解決了SLF4J警告的問題,但日誌記錄不起作用。我添加的日誌記錄不會以logback.groovy中指定的模式打印到STDOUT。日誌也沒有被添加到文件中。有人能指引我朝着正確的方向嗎?

回答

0

Gradle自帶日誌框架,您不能真正替換/添加到。最好的選擇是讓你在你的Gradle插件中使用Gradle記錄器。