本文介绍了运行由shade插件构建的独立应用程序时找不到Log4j2配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应用程序,当我从maven log4j2运行它正在工作:

I have application which when I run from maven log4j2 it is working:

mvn exec:java -Dexec.args=...

但是当我将jar作为独立应用程序运行时,它会显示错误:

but when I run jar as standalone application then it shows error:

java -jar

log:

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配置文件,但也抱怨某些格式可能在配置文件中

I dont understand this error. It shows that log4j2 configuration file is not found but also complain about some format which is probably in configuration file

我的配置是:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="off">
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d [%t] %-5p - %-26.26c{1} - %m\n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="console" />
        </Root>

        <Logger name="my.package" level="DEBUG" /> 

    </Loggers>
</Configuration>

并且它位于jar文件的根目录中。

and it is located in root directory of jar file.

UPDATE

jar由maven shade插件创建:

jar is created by maven shade plugin:

        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>

所以它包含所有必需的库(大约23 MB),当我运行这个jar时我只需要指定参数

so it contains all necessary libraries (about 23 MB) and when I run this jar I just need to specify arguments

推荐答案

好的我发现这个。

简而言之,当使用maven将应用程序类打包在uber jar中时会出现问题阴影插件。
虽然对于 log4j2 版本 2.8.1 该修复程序仍处于待定状态,建议的解决方法是更新maven pom.xml 具有阴影插件的额外配置设置如下:

In short, the problem arises when application classes are packaged in uber jar using maven shade plugin. While for log4j2 version 2.8.1 the fix is still pending, the suggested workaround is to update maven pom.xml with extra configuration settings for shade plugin as follow:

    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

        . . . . .

        <build>
            . . . . . 
            <plugins>
                . . . . . 
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.4.1</version>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <transformers>
                            <transformer
                                    implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer"/>
                        </transformers>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                        </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>
        . . . . . 
        </build>
        . . . . . 
        <pluginRepositories>
            <pluginRepository>
                <id>oss.sonatype.org</id>
                <name>OSS Sonatype Staging</name>
                <url>https://oss.sonatype.org/content/groups/staging</url>
            </pluginRepository>
        </pluginRepositories>      
        . . . . . 
    </project>

这篇关于运行由shade插件构建的独立应用程序时找不到Log4j2配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:29