在运行fatjar时无法加载log4j2

ata*_*rno 8 java logging flume log4j2 flume-ng

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

java -Dlog4j.debug=true -Dlog4j.configurationFile=/home/aaa/log4j2.xml -jar /home/aaa/myjar-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)

例外:

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.
Run Code Online (Sandbox Code Playgroud)

我甚至不知道那些[线程]来自哪里,因为即使在我的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>
Run Code Online (Sandbox Code Playgroud)

欢迎任何想法.谢谢.

laz*_*lev 8

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

不幸的是,目前似乎只有 maven-shade-plugin 的解决方案: 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>
Run Code Online (Sandbox Code Playgroud)


小智 5

在 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
}
Run Code Online (Sandbox Code Playgroud)