Maven项目Log4j2转换符未识别错误求助(已尝试常规方案)
Log4j2格式转换符识别失败问题(常规方案无效)
在Maven项目中遇到了与帖子《log4j2 ERROR StatusLogger Unrecognized conversion specifier》描述一致的问题,但已经尝试了帖子中的两种解决方案——排除Log4j2Plugins.dat文件、使用Log4j的清单转换器插件,问题仍未解决。此外已确认生成的Jar文件中,无论META-INF目录还是其他结构下,均不存在Log4j2Plugins.dat文件。
错误日志
2024-06-22T00:41:49.937Z main ERROR Unrecognized format specifier [d] 2024-06-22T00:41:49.952Z main ERROR Unrecognized conversion specifier [d] starting at position 16 in conversion pattern. 2024-06-22T00:41:49.953Z main ERROR Unrecognized format specifier [thread] 2024-06-22T00:41:49.953Z main ERROR Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern. 2024-06-22T00:41:49.954Z main ERROR Unrecognized format specifier [level] 2024-06-22T00:41:49.954Z main ERROR Unrecognized conversion specifier [level] starting at position 35 in conversion pattern. 2024-06-22T00:41:49.954Z main ERROR Unrecognized format specifier [logger] 2024-06-22T00:41:49.954Z main ERROR Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern. 2024-06-22T00:41:49.955Z main ERROR Unrecognized format specifier [msg] 2024-06-22T00:41:49.955Z main ERROR Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern. 2024-06-22T00:41:49.955Z main ERROR Unrecognized format specifier [n] 2024-06-22T00:41:49.956Z main ERROR Unrecognized conversion specifier [n] starting at position 56 in conversion pattern. 2024-06-22T00:41:49.964Z main ERROR Unrecognized format specifier [d] 2024-06-22T00:41:49.964Z main ERROR Unrecognized conversion specifier [d] starting at position 16 in conversion pattern. 2024-06-22T00:41:49.964Z main ERROR Unrecognized format specifier [thread] 2024-06-22T00:41:49.965Z main ERROR Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern. 2024-06-22T00:41:49.965Z main ERROR Unrecognized format specifier [level] 2024-06-22T00:41:49.965Z main ERROR Unrecognized conversion specifier [level] starting at position 35 in conversion pattern. 2024-06-22T00:41:49.965Z main ERROR Unrecognized format specifier [logger] 2024-06-22T00:41:49.965Z main ERROR Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern. 2024-06-22T00:41:49.965Z main ERROR Unrecognized format specifier [msg] 2024-06-22T00:41:49.965Z main ERROR Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern. 2024-06-22T00:41:49.965Z main ERROR Unrecognized format specifier [n] 2024-06-22T00:41:49.966Z main ERROR Unrecognized conversion specifier [n] starting at position 56 in conversion pattern. 2024-06-22T00:41:51.876Z main ERROR Unrecognized format specifier [p] 2024-06-22T00:41:51.876Z main ERROR Unrecognized conversion specifier [p] starting at position 4 in conversion pattern. 2024-06-22T00:41:51.877Z main ERROR Unrecognized format specifier [d] 2024-06-22T00:41:51.877Z main ERROR Unrecognized conversion specifier [d] starting at position 21 in conversion pattern. 2024-06-22T00:41:51.877Z main ERROR Unrecognized format specifier [C] 2024-06-22T00:41:51.877Z main ERROR Unrecognized conversion specifier [C] starting at position 27 in conversion pattern. 2024-06-22T00:41:51.877Z main ERROR Unrecognized format specifier [m] 2024-06-22T00:41:51.877Z main ERROR Unrecognized conversion specifier [m] starting at position 32 in conversion pattern. 2024-06-22T00:41:51.877Z main ERROR Unrecognized format specifier [n] 2024-06-22T00:41:51.877Z main ERROR Unrecognized conversion specifier [n] starting at position 35 in conversion pattern.
相关Maven pom片段
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-transform-maven-shade-plugin-extensions</artifactId> <version>0.1.0</version> </dependency> </dependencies> <executions> <execution> <id>package-jar</id> <goals> <goal>shade</goal> </goals> <phase>package</phase> <configuration> <minimizeJar>true</minimizeJar> <createDependencyReducedPom>false</createDependencyReducedPom> <artifactSet> <excludes> <exclude>org.broadinstitute.gatk:gsalib:tar.gz:*</exclude> <exclude>org.broadinstitute.gatk:*:tar.bz2:example-resources</exclude> </excludes> </artifactSet> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/services/javax.annotation.processing.Processor</exclude> <exclude>**/Log4j2Plugins.dat</exclude> </excludes> </filter> </filters> <transformers> <transformer implementation="org.apache.logging.log4j.maven.plugins.shade.transformer.Log4j2PluginCacheFileTransformer"/> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Multi-Release>true</Multi-Release> <Main-Class>${app.main.class}</Main-Class> </manifestEntries> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>${resource.bundle.path}</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
排查建议
- 关闭Jar最小化:当前启用了
<minimizeJar>true</minimizeJar>,该配置会自动移除未被检测到引用的类,很可能误删了Log4j2格式转换相关的核心组件。先将其设为false,重新打包运行,观察错误是否消失。 - 检查Log4j2依赖完整性:确认pom中已完整引入
log4j-core和log4j-api,且版本一致。用mvn dependency:tree查看依赖树,排查是否有其他依赖引入了不兼容的Log4j2版本。 - 验证Log4j2配置文件:检查你的log4j2配置文件(如log4j2.xml)中的转换模式语法是否正确,比如是否将
%d误写为[d];同时确认配置文件是否被正确加载到Jar包中。 - 更新Log4j转换器插件版本:当前使用的
log4j-transform-maven-shade-plugin-extensions版本为0.1.0,属于早期版本,尝试升级至最新稳定版(如0.2.0),修复可能存在的Shade兼容问题。 - 调整Transformer执行顺序:确保
Log4j2PluginCacheFileTransformer在所有Transformer中优先执行,避免后续转换操作破坏Log4j2插件缓存的生成。
内容的提问来源于stack exchange,提问作者Valentin Ruano
相关产品推荐
相关产品推荐

