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.pngimage

发布启动 一切貌似正常。 正当高兴之余,突然发现不打印日志了。很明显与我刚才的操作有关系,看来刚才解决问题的办法似乎不正确。静下来分析问题的根本原因:
看看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.pngimage

默认用了slf4j-log4j12 里面的StaticLoggerBinder 类,而没有用logback的 所以才会报上面的启动错误。

找到问题原因了 下面就是排包
http://ata2-img.cn-hangzhou.img-pub.aliyun-inc.com/1c7a0bf723ae52db221267fd93e1d7d1.pngimage

来至
com.alibaba.trip.tripspider:tripspider-httpclient:jar:1.0.0-SNAPSHOT:compile
排掉即可

org.slf4j
slf4j-log4j12

启动一切正常,久违的日志又有了。

思考:遇到问题 google一下找解决办法或许是最快的,但是有时候往往解决方案因人而异,可能并不完全正确,明白问题出现的根本原因找解决办法才是最靠谱的。

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
3月前
|
Java
SpringBoot 集成log4j2
SpringBoot 集成log4j2
SpringBoot 集成log4j2
|
2月前
|
XML Java 数据格式
【二十九】springboot整合logback实现日志管理
【二十九】springboot整合logback实现日志管理
65 1
|
6天前
|
XML Java Maven
Springboot整合与使用log4j2日志框架【详解版】
该文介绍了如何在Spring Boot中切换默认的LogBack日志系统至Log4j2。首先,需要在Maven依赖中排除`spring-boot-starter-logging`并引入`spring-boot-starter-log4j2`。其次,创建`log4j2-spring.xml`配置文件放在`src/main/resources`下,配置包括控制台和文件的日志输出、日志格式和文件切分策略。此外,可通过在不同环境的`application.yml`中指定不同的log4j2配置文件。最后,文章提到通过示例代码解释了日志格式中的各种占位符含义。
|
10天前
|
Java 数据安全/隐私保护 开发者
【SpringBoot】讲清楚日志文件&&lombok
【SpringBoot】讲清楚日志文件&&lombok
20 5
|
28天前
|
XML Java 数据库连接
Springboot中使用日志框架
Springboot中使用日志框架
|
1月前
|
Java 程序员
SpringBoot集成log4j2出现Logback configuration error detected: current ElementPath is
SpringBoot集成log4j2出现Logback configuration error detected: current ElementPath is
35 0
|
2月前
|
XML Java 开发者
【SpringBoot实战专题】「开发实战系列」全方位攻克你的技术盲区之SpringBoot整合众多日志管理系统服务starter-logging
【SpringBoot实战专题】「开发实战系列」全方位攻克你的技术盲区之SpringBoot整合众多日志管理系统服务starter-logging
19 1
|
2月前
|
SQL NoSQL Java
【七】springboot整合AOP实现日志操作
【七】springboot整合AOP实现日志操作
45 0
QGS
|
4月前
|
Java 数据库连接 Apache
Springboot日志框架logback与log4j2
Springboot日志框架logback与log4j2
QGS
42 0
|
4月前
|
存储 监控 安全
【Spring】SpringBoot日志
【Spring】SpringBoot日志