gradle用logback替换可传递依赖log4j

u7up0aaq  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(492)

是否可以将log4j从可传递依赖项替换为logback?这是我的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"
        }
    }

我的案子:
我刚到grad尔
从事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.
gzszwxb4

gzszwxb41#

我不确定这是最好的还是“正确”的答案。但至少 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"
        }
    }
}

相关问题