SpringBoot 升级到 2.1 后,启动程序时控制台不打印 API 的解决方法及一些感想

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 apiSpringBoot 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 搜一下,也许会有意外收获呢 :>

猜你喜欢

转载自www.cnblogs.com/VitoYi/p/10468663.html