我正在尝试在使用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 :(得分:0)
Gradle带有自己的日志框架,您无法真正替换/添加。最好的办法是在Gradle插件中使用Gradle记录器。