log4j2错误:通过Gradle生成运行jar - log4j:WARN没有找到记录器的appender

时间:2017-05-09 20:00:13

标签: gradle jar log4j2

注意:已编辑(文件名/包名已更改)

$>> java -jar build\lib\somePackageName.jar 
log4j:WARN No appenders could be found for logger (xxx.xxx.xxxx.xxxxx). 
log4j:WARN Please initialize the log4j system properly. 
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

验证jar有log4j2.xml:

$>> jar tf build\libs\somePackageName.jar  | ack -i "log4j2" 
META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat 
META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat 
log4j2.xml

使用依赖项构建Jar的Gradle任务:

sourceSets {
    main {
        resources {
            srcDirs = ['src/resources']
        }
    }
}

task someFatJar(type: Jar) {
    manifest {
        attributes (
            'Implementation-Title': 'xxxxx-xxxxxxxx',  
            'Implementation-Version': 0.x,
            'Main-Class': 'xxx.xxxxx.xxxxx.xxxxxxxxxxxx'
        )
    }
    baseName = 'someJarName'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

项目树: SRC \主\ java的\ someJavaFile.java SRC \资源\ log4j2.xml

(以防万一,我尝试移动main下的“resources”目录)

帮助!

2 个答案:

答案 0 :(得分:1)

解决方案: 使用slf4j + log4j2依赖是问题!如下所示替换了依赖项(不要使用slf4j-log4j-impl),使用log4j2中提供的一次(log4j-slf4j-impl):

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.25'
    compile 'org.apache.logging.log4j:log4j-api:2.8.2'
    compile 'org.apache.logging.log4j:log4j-core:2.8.2'
    compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.8.2'
}

答案 1 :(得分:0)

看起来像log4j:WARN的错误消息是由旧的log4j 1.2实现生成的。您需要删除该依赖项并将其替换为log4j-1.2-api适配器。这样,您可以使用Log4j2作为编码到log4j 1.2 API的应用程序的日志记录后端。