当前位置: 首页 > 面试题库 >

运行由Shade插件构建的独立应用程序时找不到Log4j2配置

胡野
2023-03-14
问题内容

我有从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配置文件,但还抱怨配置文件中可能存在某种格式

我的配置是:

<?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文件的根目录中。

更新

jar由maven shade插件创建:

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

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


问题答案:

好吧,我发现这个问题,这个问题。

简而言之,当使用maven shade plugin将应用程序类打包在uber
jar中时,就会出现问题。虽然该log4j2版本2.8.1的修复程序仍在进行中,但建议的解决方法是pom.xml使用阴影插件的额外配置设置来更新Maven
,如下所示:

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


 类似资料:
  • 我试图将grails独立插件与一个新的grails项目一起使用,但我无法使其工作。 我已将其作为插件依赖项添加到文件中: 但是,当我尝试运行时,会出现以下错误: Mac OS X 10.9.5 JDK 1.8.0_05 与GVM一起安装的Grails v2.4.4

  • 现在假设我们想要使用 Spark API 写一个独立的应用程序。我们将通过使用 Scala(用 SBT),Java(用 Maven) 和 Python 写一个简单的应用程序来学习。 我们用 Scala 创建一个非常简单的 Spark 应用程序。如此简单,事实上它的名字叫 SimpleApp.scala: /* SimpleApp.scala */ import org.apache.spark.S

  • 我有一个带有嵌入式Jetty的Spring启动应用程序,需要使用JMX监控应用程序。 只要在我的windows开发环境中启动应用程序,我就可以使用jconsole访问JMX信息(jconsole提供了一个单独连接的过程)。但是,一旦使用以下参数将应用程序作为可执行jar运行,端口将不可用,我就无法与jconsole连接 netstat-tulpn既不返回端口9010也不返回端口9011,而且我无法

  • 最近,我决定学习如何使用log4j2记录器。我下载了所需的jar文件,创建了库,xml编译文件,并尝试使用它。不幸的是,我在console(Eclipse)中得到了这样的语句: 这是我的测试类代码: 和我的xml配置文件: 我还尝试使用不带标记的xml,以及包规范和各种文件夹/包目录,但没有帮助。现在我的文件直接位于Eclipse的project文件夹中。

  • 我正在试图弄清楚如何构建一个Spring Boot独立应用程序。当然,要让东西自动连线需要一些初始的上下文起点。如果我只是尝试自动生成一个类来运行一个作业,那么即使我将它设置为静态,它也是空的。 有没有办法在一个独立的非Web应用程序中使用Spring@Services? 因此,首先将静态JobRunnerService连接到运行MyApplication的主程序,JobRunner(Servic

  • 问题内容: 我正在考虑开发由两部分组成的桌面应用程序: 用户界面(例如Java应用) 后端Node.js服务器 2个部分通过插座连接。不要问为什么我知道这很奇怪。 我将希望能够为客户提供带有安装程序的应用程序。我不希望用户必须自己安装Node.js。 有没有一种方法可以独立安装Node.js服务器,即无需在系统上全局安装Node.js。 对于任何(Windows,Linux,Mac OS X …)