3

是否可以用 logback 从传递依赖替换 log4j?这是我的 gradle 依赖项(https://github.com/geoHeil/lab04/blob/master/build.gradle):

dependencies {
    compile group: 'org.apache.hadoop', name: 'hadoop-common', version: '2.7.1'
    compile group: 'org.apache.hadoop', name: 'hadoop-client', version: '2.7.1'
    compile group: 'org.apache.hadoop', name: 'hadoop-mapreduce-client-core', version: '2.7.1'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.13'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.3'
}

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.name == 'log4j') {
            //prefer 'log4j-over-slf4j' over 'log4j', with fixed version:
            details.useTarget "ch.qos.logback:logback-classic:1.1.3"
        }
    }

我的情况:

  • 我是 Gradle 新手
  • 从事 Hadoop 项目
  • 想要使用当前的记录器,例如logback

现在,即使应该替换 log4j,它似乎也不起作用。警告仍然存在:

log4j:WARN No appenders could be found for logger 
(org.apache.hadoop.util.Shell).
log4j:WARN Please initialize the log4j system properly.
4

1 回答 1

0

我不确定这是否是最好的或“正确”的答案。但至少这些WARN消息已经消失了:

dependencies {
    compile group: 'org.apache.hadoop', name: 'hadoop-common', version: '2.7.1'
    compile group: 'org.apache.hadoop', name: 'hadoop-client', version: '2.7.1'
    compile group: 'org.apache.hadoop', name: 'hadoop-mapreduce-client-core', version: '2.7.1'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.13'
    compile group: 'org.slf4j', name: 'log4j-over-slf4j', version: '1.7.13'
    compile group: 'commons-io', name: 'commons-io', version: '2.4'
    compile 'ch.qos.logback:logback-classic:1.1.3'
}

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.name == 'slf4j-log4j12') {
            //prefer 'log4j-over-slf4j' over 'log4j', with fixed version:
            details.useTarget "ch.qos.logback:logback-classic:1.1.3"
        }
    }
}
于 2015-12-02T23:03:03.990 回答