gpt4 book ai didi

java - 运行 fatjar 时无法加载 log4j2

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:48:26 25 4
gpt4 key购买 nike

我正在从事一个使用 log4j2 日志记录的项目。在 intellij 中开发时,一切正常,日志记录按预期完成。 log4j2.xml 通过在启动时通过 intellij 设置传递给 jvm 的 java 属性进行链接。但是一旦我尝试运行一个独立的 gradle 构建的 fat-jar,我遇到了以下问题:

java -Dlog4j.debug=true -Dlog4j.configurationFile=/home/aaa/log4j2.xml -jar /home/aaa/myjar-SNAPSHOT.jar

异常(exception):

ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
...
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

我什至不明白那些[线程]是从哪里来的,因为即使在我的 log4j2 中使用最简单的基本配置时我也会遇到同样的错误:

<?xml version="1.0" encoding="UTF-8" ?><Configuration status="WARN" monitorInterval="86400">
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout
pattern="%-5p %d{yyyy-MM-dd HH:mm:ss.SSS} ${hostName} %c{1} %msg %throwable{7}%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<AppenderRef ref="console-log"/>
</Root>
</Loggers>

欢迎任何想法。谢谢。

最佳答案

在 fatJar 中,依赖项可以在导致此问题的 META-INF 中提供 log4j-provider.properties,

在 gradle 任务中删除它:

task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'project',
'Implementation-Version': project.version,
'Main-Class': 'com.sample.CLI'
}
baseName = project.name + '-all'
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it).matching {
exclude 'META-INF/**.RSA'
exclude 'META-INF/MANIFEST.MF'
exclude 'META-INF/log4j-provider.properties'
} } }
with jar
}

关于java - 运行 fatjar 时无法加载 log4j2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27361052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com