SpringBoot项目启动报错:LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Eithe

使用SpringBoot3.0.9开发,引入依赖

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>6.3.0</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>3.0.9</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>3.0.9</version>
</dependency>

SpringBoot项目启动报错: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation……
就是说Logback已经在classpath中存在,但LoggerFactory并不是Logback的相关日志上下文。此时就应该意识到有Logback依赖冲突。

解决方法,再添加依赖:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.16</version>
</dependency>
### 解决方案 当遇到`LoggerFactory is not a Logback LoggerContext but Logback is on the classpath`错误时,表明存在多个日志框架之间的冲突。具体来说,在同一个classpath下同时出现了Logback和其他的日志实现(如log4j),这会导致SLF4J无法正确初始化其上下文环境。 为了消除这种冲突并确保仅使用Logback作为唯一的日志记录工具,可以采取以下措施: #### 方法一:移除其他日志框架依赖 如果项目中已经引入了Logback,则应考虑去除其他的日志库依赖项,比如log4j或log4j2。可以通过编辑项目的构建文件来完成这一操作。对于Maven项目而言,这意味着修改`pom.xml`文件[^3]。 ```xml <!-- 移除 log4j 或者 spring-boot-starter-log4j2 的依赖 --> <dependencies> <!-- 如果有如下依赖则删除 --> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-log4j2</artifactId>--> <!--</dependency>--> <!-- 添加 logback 依赖 (如果有需要的话) --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> </dependencies> ``` #### 方法二:排除传递性的日志依赖 有时即使没有显式声明某些日志库的依赖关系,它们也可能通过第三方库间接被加入到项目当中。此时应当利用Maven提供的Dependency Analyzer功能查找这些隐藏的依赖,并对其进行排除处理[^4]。 ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <!-- 排除 slf4j 对 log4j 的绑定 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> <scope>provided</scope> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> ... </dependencies> </dependencyManagement> ``` 以上两种方法都可以有效解决由于多日志框架共存所引发的问题。建议先尝试第一种更为简单直接的方式;若未能成功解决问题再进一步排查是否有隐式的依赖需要排除。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁云亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值