运行 fatjar 时无法加载 log4j2

2024-04-08

我正在开发一个使用 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

例外情况:

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>

欢迎任何想法。谢谢。


问题描述如下:https://issues.apache.org/jira/browse/LOG4J2-673 https://issues.apache.org/jira/browse/LOG4J2-673

不幸的是,目前似乎只有 maven-shade-plugin 的解决方案:https://github.com/edwgiz/maven-shaded-log4j-transformer https://github.com/edwgiz/maven-shaded-log4j-transformer

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <finalName>${project.artifactId}${appSuffix}</finalName>
                        <transformers>
...
                            <transformer
                                    implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer">
                            </transformer>
                        </transformers>
...
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.github.edwgiz</groupId>
                    <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
                    <version>2.1</version>
                </dependency>
            </dependencies>
        </plugin>
</plugins>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

运行 fatjar 时无法加载 log4j2 的相关文章

随机推荐