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一下找解决办法或许是最快的,但是有时候往往解决方案因人而异,可能并不完全正确,明白问题出现的根本原因找解决办法才是最靠谱的。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
探讨Java中的父子类转化问题
有两个问题: (1)子类对象可以转化为父类对象吗? (2)父类对象可以转化为子类对象吗? ------------------------------------------------------------------------------------------------------------------------------------ 第(1)个问题应该都不陌生,也没什么好探讨的,肯定可以,因为在多态中经常用到。 如:class Father{} calss Son extends publc Father{} Father f = new Son(); //父类引用指向子类对象 其中,new Son()表示在在堆中分配了一段空间来存放类Son中的数据, 并返回一个Son的对象,并赋值给Father的引用f,即f指向子类的对象, 此时,子类的对象并没有定义一个名字。 定价于: Son s = new Son(); Father f = s; ------------------------------------------------------...
- 下一篇
你真的了解博客园的目录么。。
前言 事情是这样的,最近忙着软件测试没注意博客园的消息,今天无意间点开看到这个: 非常感谢这位盆友能发现这个问题,奖励鸡腿,这是那篇博文:要嫁就嫁程序员,因为。。。 恩?,博客园的目录设置在手机端居然有问题,一直都用电脑没注意,我赶紧用手机点开一看 好端端的文章全部被目录给遮住了,影响阅读,很影响心情啊;我赶快找找解决办法; 目前我学会了目录的四种形式: 1.目录在侧边栏: 示例:https://www.cnblogs.com/clwydjgs/p/9290075.html 也就是我现在用的这个目录,我让目录在侧边栏显示,这样手机端不受任何影响,只是网页端的美化效果没有之前的好; 方法: 在页首HTML中加入: 1 <link href="http://files.cnblogs.com/files/clwydjgs/cnblog-scroller.css" type="text/css" rel="stylesheet"> 2 <script src="http://files.cnblogs.com/files/clwydjgs/scrollspy.js" typ...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7设置SWAP分区,小内存服务器的救世主
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- Windows10,CentOS7,CentOS8安装Nodejs环境
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS8安装Docker,最新的服务器搭配容器使用
- 设置Eclipse缩进为4个空格,增强代码规范