使用log4j-api与log4j-core 2.17.1时SpringBoot控制台报错的解决咨询
解决Log4j2找不到日志实现的控制台报错问题
出现ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...报错,本质是SpringBoot默认日志组件与你引入的Log4j2存在依赖冲突或配置不完整,按以下步骤解决:
一、修正Maven依赖配置
SpringBoot默认集成spring-boot-starter-logging(SLF4J+Logback),需先排除该依赖,再引入Log4j2的starter包:
<!-- 排除默认的Logback日志依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- 引入Log4j2 starter(已包含log4j-api、log4j-core及SLF4J绑定) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> <version>2.17.1</version> </dependency>
无需单独引入
log4j-api和log4j-core,spring-boot-starter-log4j2已包含这两个组件,避免重复引入导致版本不一致。
二、修正Gradle依赖配置
先排除默认日志依赖,再添加Log4j2 starter:
dependencies { implementation('org.springframework.boot:spring-boot-starter') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' } implementation 'org.springframework.boot:spring-boot-starter-log4j2:2.17.1' }
三、额外排查步骤
- 检查依赖冲突:执行
mvn dependency:tree(Maven)或./gradlew dependencies(Gradle)命令,查看是否有其他依赖间接引入了Logback或旧版本Log4j,如有需针对性排除。 - 添加Log4j2配置文件:在
src/main/resources下创建log4j2.xml或log4j2.properties配置文件,自定义日志输出规则。即使没有配置文件,Log4j2也会使用默认配置,但添加配置能更好控制日志行为。
内容的提问来源于stack exchange,提问作者JackeyLove
相关产品推荐
相关产品推荐

