springboot 日志问题记录

在使用Spring Boot框架开发过程中,遇到因Logback与Log4j同时存在导致的日志工厂冲突错误。文章详细记录了错误堆栈信息,指出在Spring Boot启动时出现java.lang.IllegalArgumentException异常,提示LoggerFactory不是一个LogbackLoggerContext。为解决此问题,需移除Logback或Log4j,若在WebLogic环境中,则需在WEB-INF/weblogic.xml中添加'org.slf4j'到prefer-application-packages。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:新建工程busr,采用pandora boot,引入了需要的包,简单写了点代码发布,
spring-boot启动报错:

Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. 
Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from 
jar:file:/opt/**.jar!/BOOT-INF/lib/log4j-slf4j-impl-2.6.2.jar!/). If you are using WebLogic you will need to add 'org.slf4j' 
to prefer-application-packages in WEB-INF/weblogic.xml Object of class [org.apache.logging.slf4j.Log4jLoggerFactory] 
must be an instance of class ch.qos.logback.classic.LoggerContext
at org.springframework.util.Assert.isInstanceOf(Assert.java:346)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:231)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:97)
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationStartedEvent(LoggingApplicationListener.java:226)
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:205)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:121)
at org.springframework.boot.context.event.EventPublishingRunListener.started(EventPublishingRunListener.java:63)
at org.springframework.boot.SpringApplicationRunListeners.started(SpringApplicationRunListeners.java:48)

转载于:https://my.oschina.net/u/3611008/blog/2209510

### 配置 Spring Boot 记录 SQL 执行情况 在 Spring Boot 应用程序中,可以通过多种方式配置日志记录 SQL 语句的执行情况。以下是详细的说明: #### 使用默认日志框架(Logback) Spring Boot 默认集成了 Logback 作为其日志框架。为了启用 SQL 日志记录,可以调整 `application.properties` 或 `application.yml` 文件中的日志级别设置。 1. **针对 Hibernate 的 SQL 日志** 如果使用的是 Spring Data JPA 和 Hibernate,则可以在 `application.properties` 中添加以下配置: ```properties logging.level.org.hibernate.SQL=debug logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace ``` 上述配置分别启用了 SQL 查询的日志记录和绑定参数的日志记录[^1]。 2. **针对 MyBatis 的 SQL 日志** 对于基于 MyBatis 的项目,同样可以通过修改日志级别来实现 SQL 输出的功能。具体来说,在 `application.properties` 中加入如下内容即可: ```properties logging.level.com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor=debug ``` 此外,如果希望进一步增强对数据库操作细节的理解,还可以引入 Druid 数据源并为其单独定义 logger: ```xml <logger name="druid.sql.Statement" level="DEBUG" additivity="false"/> ``` 这一 XML 片段应被追加至项目的 `logback-spring.xml` 文件之中[^5]。 3. **自定义 Logger 设置** 当上述通用方法无法满足特定需求时,可考虑创建专属的日志类别或者利用 AOP 技术拦截目标服务层函数调用链路从而完成更加精细的操作跟踪[^2]。 4. **验证效果** 完成以上更改之后重新运行您的 springboot 工程,此时终端应该能够展示出每次访问数据表所对应的原始 sql 文本连同实际传入变量值一同呈现出来便于排查问题所在之处[^4]。 ```java // 示例:测试类用于确认日志输出正常工作的简单例子 @SpringBootTest public class SqlLoggingTest { @Autowired private YourRepository repository; @Test void testSqlLogs() { List<YourEntity> entities = repository.findAll(); assertNotNull(entities); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值