Spring Boot误将log4j2.xml识别为Logback配置文件的解决求助
日志配置问题排查与解决
问题背景
Spring Boot 2.7.6应用配置log4j2.xml作为日志配置,已排除spring-boot-starter-logging并引入spring-boot-starter-log4j2,但启动时出现两类错误:
- 初始错误:Spring Boot将
log4j2.xml当作Logback配置文件解析,排查后发现liquibase-core引入了logback-classic,排除后仍报错,且dependency:tree已无logback-classic依赖。 - 后续错误:排除Logback后出现
ERROR StatusLogger Unable to create Lookup for ctx,提示缺失org/apache/logging/log4j/util/ServiceLoaderUtil类。
相关配置
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Extra logging related to initialization of Log4j. Set to debug or trace if log4j initialization is failing. --> <Configuration status="warn"> <Appenders> <!-- Console appender configuration --> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </Console> </Appenders> <Loggers> <!-- Root logger referring to console appender --> <Root level="info" additivity="false"> <AppenderRef ref="console" /> </Root> </Loggers> </Configuration>
pom.xml关键依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.6</version> </parent> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>${log4j-slf4j-impl.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j-core.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-layout-template-json</artifactId> <version>2.19.0</version> <scope>runtime</scope> </dependency>
错误信息
初始错误
Logging system failed to initialize using configuration from 'classpath:log4j2.xml' java.lang.IllegalStateException: Logback configuration error detected: ERROR in ch.qos.logback.core.joran.spi.Interpreter@3:17 - no applicable action for [Properties], current ElementPath is [[Configuration][Properties]] ...
排除Logback后的错误
ERROR StatusLogger Unable to create Lookup for ctx java.lang.NoClassDefFoundError: org/apache/logging/log4j/util/ServiceLoaderUtil at org.apache.logging.log4j.core.impl.ThreadContextDataInjector.getServiceProviders(ThreadContextDataInjector.java:77)
解决方案
1. 彻底解决Logback解析log4j2.xml问题
- 强制指定日志系统:在
application.properties或application.yml中添加配置,明确告诉Spring Boot使用Log4j2:spring.main.logging-system=org.springframework.boot.logging.log4j2.Log4J2LoggingSystem logging.config=classpath:log4j2.xml - 排查残余Logback依赖:执行命令
mvn dependency:tree -Dincludes=ch.qos.logback,检查是否还有其他依赖引入Logback,如有则对应排除。比如liquibase-core的排除配置:<dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> </exclusions> </dependency>
2. 解决ServiceLoaderUtil缺失问题
- 移除手动指定的Log4j核心依赖:
spring-boot-starter-log4j2已经自动管理了log4j-core、log4j-slf4j-impl等依赖的版本,手动指定版本会导致版本不兼容。删除pom中以下依赖:<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>${log4j-slf4j-impl.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j-core.version}</version> </dependency> - 对齐log4j-layout-template-json版本:Spring Boot 2.7.6对应的Log4j2版本是2.17.1,将
log4j-layout-template-json的版本改为2.17.1,或直接使用parent中定义的${log4j2.version}属性:<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-layout-template-json</artifactId> <version>${log4j2.version}</version> <scope>runtime</scope> </dependency> - 更新依赖并清理缓存:执行
mvn clean install清理旧依赖并重新构建项目。
内容的提问来源于stack exchange,提问作者gstackoverflow
相关产品推荐
相关产品推荐

