1、問題描述
我將我的 SpringBoot 版本由 2.0.5.RELEASE 升級到 2.1.3,發現在項目啟動的時候,控制台不打印 API 了。
應該不是日志級別而是配置的問題,我嘗試調整過日志級別,發現並沒有卵用(其實是有用的,只不過樣式變了,一開始沒發覺)。
將版本再切換回 2.0.5.RELEASE, 就能正常打印 API 了。
2、解決方法
配置文件中更改 org.springframework.web 包的日志級別:
logging:
level:
org.springframework.web: TRACE
啟動程序時將會在控制條打印出如下信息:
2019-03-04 02:20:47.554 TRACE 13549 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping :
c.s.q.c.AccountBookController:
{POST /accountBook/addAccountBook}: addAccountBook(AccountBookDTO,BindingResult,String)
{GET /accountBook/overview}: overview(String)
{GET /accountBook/loanDetail/{id}}: loanDetail(int)
{POST /accountBook/repayment/{id}}: repayment(int)
2019-03-04 02:20:47.561 TRACE 13549 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping :
c.s.q.c.PortalMessageController:
{GET /message/xxx}: xxx()
{GET /message/xxx}: xxx(String)
3、解決思路
首先百度是怎么都沒能找到答案,倒是在百度知道上看到了一個提相同問題的人。
沒辦法,我只能用 poor English 在 Google 搜索:SpringBoot do not print api、SpringBoot not print url, 倒還真被我找到了:eclipse spring boot console log does not print mapped controller info
內容大概提到了,Spring Boot 2.1 使用了 Spring Framework 5.1, 而 Spring Framework 5.1 對日志做了較大的改動。
現在使用 INFO 級別記錄的信息非常少,DEBUG 級別提供了更多信息,但不詳細。
只有 TRACE 級別才會提供詳細信息。
所以,我跑到了 GitHub 去翻看了 SpringBoot 的 Wiki, V2.1 中也提到了:
Spring Framework 5.1 revisited the debug logging output while working on web applications (Spring MVC or Spring WebFlux). If you are trying to debug an application and you want to restore Spring Boot 2.0 style logging you should add the following to your application.properties:
...
4、一些感想
首先呢,就是既然要使用新版本,最好還是要去看看人家 Wiki, 留意一下新特性。就算不用新版本,但既然是做這一行的,對於這種更新同樣也要多留意一下;
其次呢,如果有些問題百度不出來,就算使用再拙劣的英文,也要嘗試去 Google、StackOverflow 搜一下,也許會有意外收獲呢 :>