問題描述:springboot啟動異常,啟動后沒有日志打印。
問題原因:slf4j日志實現重復,找不到對應實現類。
問題應對:
1. 是不是項目沒起來---->打印的日志數據,到這里就不打印了,在run方法后面加了一行輸入print,當啟動之后使用debug,能運行到此行,說明正常啟動成功的。
2.是不是日志框架沖突導致的--->把日志相關的pom以來都注釋,發現print的能打印出來,log.info 相關的打印不出來。
把綁定的兩塊實現類都找了一下:
[jar:file:/D:/ruanjian/apache-maven-3.0.3-NEW/repository/org/slf4j/slf4j-log4j12/1.7.32/slf4j-log4j12-1.7.32.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/ruanjian/apache-maven-3.0.3-NEW/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.14.1/log4j-slf4j-impl-2.14.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
2.1 繼續跟下去 slf4j-log4j12 這么搶手,好幾處都用到了它
spring-boot-starter-log4j2 依賴的是這個版本log4j-slf4j-impl
SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/D:/ruanjian/apache-maven-3.0.3-NEW/repository/org/slf4j/slf4j-log4j12/1.7.32/slf4j-log4j12-1.7.32.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/D:/ruanjian/apache-maven-3.0.3-NEW/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.14.1/log4j-slf4j-impl-2.14.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] log4j:WARN No appenders could be found for logger (org.springframework.boot.ApplicationServletEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.3)
3.1 問題找到了,解決方案開始實施:
我這里就把對應的日志:排除掉了,目前排除的是這個:
spring-boot-starter-log4j2 依賴的是這個版本log4j-slf4j-impl ,百度到的消息說這是個橋接包
日志框架:slf4j
日志實現:log4j2
橋接包:log4j-slf4j-impl
橋接包log4j-slf4j-impl起到適配的作用,因為市面上的日志實現互不兼容,日志框架slf4j要想適用於日志實現log4j2,就需要使用橋接包
具體啥情況咱也不太懂,反正我是認為排除一個依賴就該能運行了吧? 當時是在這里
spring-boot-starter-web 進行的依賴排除,我發現並沒有排除掉依賴,我就百度搜了一下,然后從springboot的基礎上進行排除
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>
依賴排除掉之后發現並沒有達到預期的結果,還是這樣,除了爆紅的幾條明晃晃的信息之外,沒有其他的消息了,調用輸出的日志信息也沒有正常顯示出來,通過debug也沒有異常,我慌了,
然后進行百度,
No appenders could be found for logger (org.springframework.boot.ApplicationServletEnvironment).
這條有效信息進行百度的結果都是spring的方式啟動的,然后都是提示讓本地加log的properties文件,嘗試的加上了,結果還是老樣子。
第二條有效信息進行百度:
Please initialize the log4j system properly.
log4j:WARN No appenders could be found for logger (org.springframework.boot.ApplicationServletEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
(54條消息) No appenders could be found for logger_u013412790的專欄-CSDN博客
按照博客的說明,我按照第二條的的方式,把配置加到項目最上方
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-1.2-api</artifactId> <version>2.8.2</version> </dependency>
啟動之后,發現日志能正常打印出來了,心里別提多高興了!
接下來,看看是否還需要配置文件,把配置文件刪除,發現還能正常使用。到此處問題已經解決。
最后:不光要解決問題,以后遇到類似的問題要知道怎么解決和排查,要知其人並知其所以然。
找到了官方網站:http://www.slf4j.org/manual.html
日志的實現框架大概有以下幾種:那么 slf4j-log4j12是哪個日志框架呢?
點擊進去發現說: http://logging.apache.org/log4j/1.2/index.html
2015年8月5日,伐木服務項目管理委員會宣布Log4j 1.x已結束使用。有關公告的完整文本,請參閱阿帕奇博客。建議 Log4j 1 的用戶升級到阿帕奇日志 4j 2。
點進去:http://logging.apache.org/log4j/2.x/index.html
看導入pom是這樣導入的
<dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.14.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.14.1</version> </dependency> </dependencies>
--------------------------------------------------------------------------------------------------------------------------
上面的因為日志找不到而導致的異常,今天我就想把日志集成到
log4j2版本上去,發現了奇怪的一幕,目前還是兩個實現類,而且實現的是隨機使用一個實現類
SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/D:/ruanjian/apache-maven-3.0.3-NEW/repository/ch/qos/logback/logback-classic/1.2.4/logback-classic-1.2.4.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/D:/ruanjian/apache-maven-3.0.3-NEW/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.14.1/log4j-slf4j-impl-2.14.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
配置輸出日志:
logging.level.root=INFO
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}[%line] TraceId[%X{PFTID}] - %msg%n