运行由shade插件构建的独立应用程序时找不到Log4j2配置
我有应用程序,当我从maven log4j2运行时它正在工作:
mvn exec:java -Dexec.args=...
但是当我将jar作为独立应用程序运行时,它会显示错误:
java -jar
日志:
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 Unrecognized format specifier [level] ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern. ERROR StatusLogger Unrecognized format specifier [logger] ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern. ERROR StatusLogger Unrecognized format specifier [msg] ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern. ERROR StatusLogger Unrecognized format specifier [n] ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern. ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. 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 Unrecognized format specifier [level] ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern. ERROR StatusLogger Unrecognized format specifier [logger] ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern. ERROR StatusLogger Unrecognized format specifier [msg] ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern. ERROR StatusLogger Unrecognized format specifier [n] ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.
我不明白这个错误。 它显示找不到log4j2配置文件,但也抱怨某些格式可能在配置文件中
我的配置是:
它位于jar文件的根目录下。
UPDATE
jar由maven shade插件创建:
org.apache.maven.plugins maven-shade-plugin
所以它包含所有必需的库(大约23 MB),当我运行这个jar时,我只需要指定参数
好的,我发现这个问题。
简而言之,当使用maven shade插件将应用程序类打包在超级jar中时会出现问题。 虽然对于log4j2
版本2.8.1
,修复程序仍处于待定状态,建议的解决方法是使用树荫插件的额外配置设置更新maven pom.xml
,如下所示:
. . . . . . . . . . . . . . . org.apache.maven.plugins maven-shade-plugin 2.4.1 *:* META-INF/*.SF META-INF/*.DSA META-INF/*.RSA package shade com.github.edwgiz maven-shade-plugin.log4j2-cachefile-transformer 2.1 . . . . . . . . . . . . . . . oss.sonatype.org OSS Sonatype Staging https://oss.sonatype.org/content/groups/staging . . . . .
有关pom.file的完整示例,请参阅此处。
除了@hudi给出的答案之外,我们还需要在插件中添加依赖项以进行转换。
src/main/java src/main/resources org.apache.maven.plugins maven-shade-plugin 2.4.1 package shade com.auto.facade.RunMain 123 *:* META-INF/*.SF META-INF/*.DSA META-INF/*.RSA com.github.edwgiz maven-shade-plugin.log4j2-cachefile-transformer 2.6.1 maven-compiler-plugin 3.1 1.7 1.7
org.apache.maven.plugins maven-shade-plugin 2.4.3 package shade *:* META-INF/*.SF META-INF/*.DSA META-INF/*.RSA project-name-product-1.0.0-SNAPSHOT your.main.classname META-INF/spring.handlers META-INF/spring.schemas META-INF/spring.tooling com.github.edwgiz maven-shade-plugin.log4j2-cachefile-transformer 2.6.1