在运行fatjar时无法加载log4j2

问题描述 投票:7回答:4

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

例外:

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>

欢迎任何想法。谢谢。

java logging log4j2 flume flume-ng
4个回答
4
投票

在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
}

2
投票

问题在这里描述: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>

1
投票

LoggerContextFactory将Log4j API绑定到它的实现。 Log4j LogManager通过查找META-INF/log4j-provider.properties(标准java.util.Properties文件)的所有实例来定位LoggerContextFactory,然后检查每个实例以验证它是否为Log4jAPIVersion属性指定了符合LogManager所需版本的值。

加入fat jar,你也可以通过以下方式明确指定log4j2在你的应用程序中使用LoggerContextFactory

System.setProperty("log4j2.loggerContextFactory", "org.apache.logging.log4j.core.impl.Log4jContextFactory")

或者在log4j-provider.properties jar中包含的log4j-core文件中指定。


0
投票

当您从IDE运行应用程序时,jar会自行运行而不嵌入依赖项,并且您没有日志设置冲突。但是当您将应用程序转换为胖jar时,所有依赖项将被注入到项目的jar文件中,而来自外部jar文件(依赖项)的log4j设置可能会发生冲突,而fatJar进程会将它们合并到单个工件中。

在这种情况下,我认为你的“Log4j2Plugins.dat”文件可能会有冲突。可以肯定的是,您可以使用zip编辑器打开fatJar文件(例如:7Zip),导航到fatJar中的路径,如下所示,并从fatJar中删除一个冲突的文件(您可以选择最小的一个)。运行fatJar并检查日志记录是否正常工作。

\ META-INF \组织\ apache的\日志\ log4j的\核心\ CONFIG \插件\ Log4j2Plugins.dat

现在我们可以检查依赖项(工件)并找到它们中的哪些包含“Log4j2Plugins.dat”文件。因此,您可以从构建工具中排除具有该文件的模块,然后您的fatJar创建过程将排除冲突的文件,并且您的新fatJar可以按预期开始记录。

在我的例子中,我的fatJar模块从Spring Boot导入了一些其他模块,当我排除冲突的日志库时,我的fatJar开始记录而没有任何错误。

configurations { all*.exclude module: 'spring-boot' all*.exclude module: 'spring-boot-starter-logging' all*.exclude module: 'logback-classic' all*.exclude module: 'commons-logging' }

© www.soinside.com 2019 - 2024. All rights reserved.