springboot 日志问题记录
问题:新建工程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)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175)
看到错误 "LoggerFactory is not a Logback LoggerContext but Logback is on the classpath.
Either remove Logback"
大概意思是 可以remove掉logback 解决问题。 当时直接Google了下 Stack Overflow上有篇文章也说
排掉logback的包即可。
于是 我这么做了
http://ata2-img.cn-hangzhou.img-pub.aliyun-inc.com/5231e3284419eb9b203064fc694ae66d.png
发布启动 一切貌似正常。 正当高兴之余,突然发现不打印日志了。很明显与我刚才的操作有关系,看来刚才解决问题的办法似乎不正确。静下来分析问题的根本原因:
看看spring boot 是怎么加载日志的:
源码中 onApplicationStartedEvent方法,在系统启动时会被调用,LoggingSystem获取当然的日志系统
private void onApplicationStartedEvent(ApplicationStartedEvent event) {
this.loggingSystem = LoggingSystem
.get(event.getSpringApplication().getClassLoader());
this.loggingSystem.beforeInitialize();
}
默认的会引入下面3个日志系统 如果没有任何配置的化 其默认的是LogbackLoggingSystem
static {
Map systems = new LinkedHashMap();
systems.put("ch.qos.logback.core.Appender",
"org.springframework.boot.logging.logback.LogbackLoggingSystem");
systems.put("org.apache.logging.log4j.core.impl.Log4jContextFactory",
"org.springframework.boot.logging.log4j2.Log4J2LoggingSystem");
systems.put("java.util.logging.LogManager",
"org.springframework.boot.logging.java.JavaLoggingSystem");
SYSTEMS = Collections.unmodifiableMap(systems);
}
逐步跟踪到第一次系统报错的地方
private LoggerContext getLoggerContext() {
ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory();
Assert.isInstanceOf(LoggerContext.class, factory,
String.format(
"LoggerFactory is not a Logback LoggerContext but Logback is on "
- "the classpath. Either remove Logback or the competing "
- "implementation (%s loaded from %s). If you are using "
- "WebLogic you will need to add 'org.slf4j' to "
- "prefer-application-packages in WEB-INF/weblogic.xml",
factory.getClass(), getLocation(factory)));
return (LoggerContext) factory;
}
说明返回的factory不是logback的实例, StaticLoggerBinder.getSingleton().getLoggerFactory() 没有找到logback,
所以这个时候开始怀疑StaticLoggerBinder 是否是因为包冲突,果然 如下图
http://ata2-img.cn-hangzhou.img-pub.aliyun-inc.com/a9fbe3063e81267c31ec925c4b5e8c35.png
默认用了slf4j-log4j12 里面的StaticLoggerBinder 类,而没有用logback的 所以才会报上面的启动错误。
找到问题原因了 下面就是排包
http://ata2-img.cn-hangzhou.img-pub.aliyun-inc.com/1c7a0bf723ae52db221267fd93e1d7d1.png
来至
com.alibaba.trip.tripspider:tripspider-httpclient:jar:1.0.0-SNAPSHOT:compile
排掉即可
org.slf4j
slf4j-log4j12
启动一切正常,久违的日志又有了。
思考:遇到问题 google一下找解决办法或许是最快的,但是有时候往往解决方案因人而异,可能并不完全正确,明白问题出现的根本原因找解决办法才是最靠谱的。
springboot 日志问题记录相关推荐
- SpringBoot使用Logbook记录HTTP请求响应日志
写在前面:2020年面试必备的Java后端进阶面试题总结了一份复习指南在Github上,内容详细,图文并茂,有需要学习的朋友可以Star一下! GitHub地址:https://github.com/ ...
- springboot 日志 log4j
日志: 记录事件发生过程,方便问题追溯,问题查找,注意中文乱码及日志级别 jar包引入: <!-- log4j --> <dependency><groupId>o ...
- Spring Boot与日志 ——日志框架、日志配置||SLF4j使用||SpringBoot日志关系||切换日志框架
1.日志框架 SLF4j使用 1.如何在系统中使用SLF4j 以后开发的时候,日志记录方法的调用,不应该来直接调用日志的实现类,而是调用日志抽象层里面的方法: 给系统里面导入slf4j的jar和 lo ...
- springboot日志配输出路径配置_SpringBoot日志配置详解
前言 记录应用系统曰志主要有三个原因 记录操作轨迹.监控系统运行状况.回溯系统故障.记录操作行为及操作轨迹数据,可以数据化地分析用户偏好,有助于优化业务逻辑,为用户提供个性化的服务.例如,通过 ac ...
- springboot 日志_Springboot与日志
1.日志框架 JUL.JCL.Jboss-logging.logback.log4j.log4j2.slf4j.... 日志门面(日志的抽象层) 日志实现 JCL(Jakarta Commons Lo ...
- SpringBoot - 日志选择与实现
SpringBoot - 日志选择与实现 [1]常见的日志门面与实现框架 日志门面 实现框架 JCL(Jakarta Commons logging),SLF4J,Jboss logging Log4 ...
- SpringBoot日志logback-spring.xml分环境
springboot按照profile进行打印日志 log4j logback slf4j区别? 首先谈到日志,我们可能听过log4j logback slf4j这三个名词,那么它们之间的关系是怎么样 ...
- SpringBoot——日志文件
基本概念 日志文件记录了程序的报错信息,执行时间,用户的登录状态,操作时间等等 通过日志,我们可以轻松的找到程序的问题,得到程序的相关信息 springBoot启动时控制台打印的这些,就是程序的日志 ...
- SpringBoot日志级别设置
SpringBoot日志级别设置 在application.properties配置文件中设置: #设置时区: spring.jackson.date-format=yyyy-MM-dd HH:mm: ...
最新文章
- Build Boost C++ libraries for x32/x64 VC++ compilers on Windows
- 报错解决:usr/bin/ld: output.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when maki
- 转:FMS 3.5之Hello World!
- hashMap与hashTable区别
- 白话Elasticsearch58-数据建模实战_基于nested object实现博客与评论嵌套关系
- pythonsql注入步骤_防止SQL注入解决方案
- 苹果CMSv10黑金色自适应网站模板
- sql中怎么根据汉字的拼音首字母查询
- Excel读取某一列的宏代码VBA代码源码及解说(详尽版)
- mybatis基于XML(二)
- 【计算机网络笔记】数据链路层(封装成帧,差错检测,可靠传输)
- VSTO/Excel: 获取Excel图表中的某个点的数据
- 数据库、连接-mysql学习笔记二-by小雨
- python爬虫实现hdu自动交题
- Humanoid(人形)动画概述——动画(Mecanim)系统学习
- 计算机等级考试二级VB基础教程
- tidyverse笔记——tidyr包
- 用Scrapy框架爬取豆瓣电影,构建豆瓣电影预测评分模型
- 干货 | 五千字长文带你快速入门FlinkSQL
- L1-020 帅到没朋友(c++包含测试点)