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

升级SLF4J至2.17.2后出现Log4j格式解析错误求助

SLF4J升级至2.17.2后日志格式错误排查

错误信息

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 DOM element is - not a <log4j:configuration> element.

已尝试的排查方案

  • 添加Maven Shade插件排除.dat文件;
  • 解压前后版本的Jar包,检查Log4j2配置文件是否被覆盖(未发现覆盖情况);
  • 对比两次安装的Maven依赖(除预期的日志版本外无其他差异)。

问题原因及解决方案

1. SLF4J 2.x与Log4j绑定依赖不匹配

SLF4J 2.x彻底弃用了旧的slf4j-log4j12绑定,必须使用log4j-slf4j-impl作为SLF4J到Log4j2的桥接,且版本必须与SLF4J严格对齐(即2.17.2)。如果项目中残留旧绑定,或者Log4j核心组件版本与SLF4J不一致,就会引发格式解析冲突。

解决步骤:

  • 移除Maven依赖中的slf4j-log4j12,添加对应版本的Log4j2绑定及核心组件:
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.17.2</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.17.2</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.17.2</version>
</dependency>
  • 执行mvn dependency:tree检查依赖树,确保没有旧版本的Log4j或SLF4J绑定被间接引入。

2. 日志配置文件使用了Log4j 1.x语法

错误中的%d、%thread等格式符是Log4j 1.x的语法,但Log4j2的转换模式规则有差异,且配置文件结构完全不同:

  • Log4j 1.x的XML配置根元素是<log4j:configuration>,而Log4j2要求根元素为<Configuration>(命名空间为http://logging.apache.org/log4j/2.0/config);
  • Log4j2的格式符缩写更简洁:thread改为t,logger改为c,msg改为m,%d需要指定格式(如%d{yyyy-MM-dd HH:mm:ss})。

解决步骤:

  • 将日志配置文件重命名为log4j2.xml或log4j2.properties(Log4j2默认识别这两个文件名);
  • 调整配置语法,比如:
    Log4j 1.x格式:
    log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
    
    改为Log4j2格式:
    appender.stdout.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %c{36} - %m%n
    

3. 类路径中存在冲突的配置文件

即使Jar包未被覆盖,类路径中可能同时存在Log4j 1.x和Log4j2的配置文件,导致Log4j2错误加载旧配置。

解决步骤:

  • 删除项目中所有Log4j 1.x的配置文件(如log4j.xml、log4j.properties);
  • 检查依赖Jar包的资源目录,确保没有携带旧版日志配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 07:20:28