You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署JAR遇Log4J转换符识别错误,求自动解决方案

问题描述

部署JAR文件时遇到以下Log4j2错误:

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.

手动删除JAR中的Log4j2Plugins.dat文件后问题可解决,但每次手动操作效率低下。当前使用Maven Assembly插件构建包含依赖的JAR,未使用Shade插件,pom.xml配置如下:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <quickfixj-core.version>2.3.1</quickfixj-core.version>
    <aws-java-sdk-kinesis.version>1.12.429</aws-java-sdk-kinesis.version>
    <gson.version>2.10.1</gson.version>
    <quickfixj-messages-fix44.version>2.3.1</quickfixj-messages-fix44.version>
    <ssm.version>2.20.38</ssm.version>
    <log4j-api.version>2.21.1</log4j-api.version>
    <amazon-kinesis-producer>0.15.6</amazon-kinesis-producer>
    <jaxb-api.version>2.3.1</jaxb-api.version>
    <json.version>20230227</json.version>
    <postgresql.version>42.6.0</postgresql.version>
    <lombok.version>1.18.26</lombok.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.quickfixj</groupId>
        <artifactId>quickfixj-core</artifactId>
        <version>${quickfixj-core.version}</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>${gson.version}</version>
    </dependency>
    <dependency>
        <groupId>org.quickfixj</groupId>
        <artifactId>quickfixj-messages-fix44</artifactId>
        <version>${quickfixj-messages-fix44.version}</version>
    </dependency>
    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>ssm</artifactId>
        <version>${ssm.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j-api.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j-api.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${log4j-api.version}</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>amazon-kinesis-producer</artifactId>
        <version>${amazon-kinesis-producer}</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>${jaxb-api.version}</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>${json.version}</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgresql.version}</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
    </dependency>
</dependencies>
<build>
    <finalName>[placeholder]-${project.version}</finalName>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.7.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <finalName>[placeholder]</finalName>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>[placeholder]</mainClass>
                    </manifest>
                </archive>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </plugin>
    </plugins>
</build>
解决方案

1. 配置Maven Assembly插件自动排除Log4j2Plugins.dat

直接修改maven-assembly-plugin的配置,添加文件排除规则,构建时自动过滤所有依赖里的Log4j2Plugins.dat文件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <finalName>[placeholder]</finalName>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>[placeholder]</mainClass>
            </manifest>
        </archive>
        <appendAssemblyId>false</appendAssemblyId>
        <!-- 添加排除规则 -->
        <excludes>
            <exclude>**/Log4j2Plugins.dat</exclude>
        </excludes>
    </configuration>
</plugin>

这样构建出来的JAR会自动去掉所有Log4j2Plugins.dat文件,不用再手动删除。

2. 禁用Log4j2的插件缓存

不想改构建配置的话,也可以通过启动参数或配置文件禁用Log4j2的插件缓存,让它动态加载插件,不再依赖预生成的Log4j2Plugins.dat:

  • 启动时加系统属性:
    启动JAR时带上这个参数:

    -Dlog4j2.plugins.cache.file=false
    
  • 修改log4j2.xml配置:
    在Log4j2的配置文件里添加disablePluginsCache属性:

    <Configuration status="WARN" disablePluginsCache="true">
        <!-- 其他配置内容 -->
    </Configuration>
    

3. 排查依赖冲突

虽然你pom里Log4j版本统一,但有些依赖可能间接引入了不同版本的Log4j组件,导致Log4j2Plugins.dat不兼容。可以用Maven命令查看依赖树:

mvn dependency:tree | grep log4j

如果发现有其他版本的Log4j依赖,用<exclusions>标签排除掉,确保所有Log4j相关依赖版本一致。


内容的提问来源于stack exchange,提问作者ExceptionExplorer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 23:09:51