log4j2 error: running jar generated via Gradle - log4j:WARN No appenders could be found for logger

扶醉桌前 提交于 2019-12-12 04:17:42

问题


Note: edited (filenames/packagenames changed)

$>> 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.

validated jar has 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

Gradle task to build Jar with dependencies:

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
}

Project Tree: src\main\java\someJavaFile.java src\resources\log4j2.xml

(just in case, i tried moving "resources" directory under main)

Help!


回答1:


Solution: using slf4j + log4j2 dependencies was the issue! replaced the dependencies as below (do not use slf4j-log4j-impl), use the once available from 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'
}



回答2:


Error messages that look like log4j:WARN are generated by the old log4j 1.2 implementation. You need to remove that dependency and replace it with the log4j-1.2-api adapter. That way you can use Log4j2 as the logging backend for applications coded to the log4j 1.2 API.



来源:https://stackoverflow.com/questions/43878869/log4j2-error-running-jar-generated-via-gradle-log4jwarn-no-appenders-could-b

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!