Spring Boot 2.1 MVC 日志记录

Posted

技术标签:

【中文标题】Spring Boot 2.1 MVC 日志记录【英文标题】:Spring Boot 2.1 MVC Logging 【发布时间】:2019-06-18 20:24:33 【问题描述】:

从 2.0.x 升级 Spring Boot 2.1.x,不记录 MVC 映射日志..

Spring Boot 2.0.x记录了INFO级别的MVC Mapping,非常方便且易于识别的日志。

**Spring Boot 2.0.x**
2019-01-24 20:10:11.165  INFO  [           main]  s.w.s.m.m.a.RequestMappingHandlerMapping  Mapped "[/mapping2],methods=[GET]" onto public java.lang.String com.test.controller.ControllerClass.method2()
2019-01-24 20:10:11.167  INFO  [           main]  s.w.s.m.m.a.RequestMappingHandlerMapping  Mapped "[/mapping1],methods=[GET]" onto public java.lang.String com.test.controller.ControllerClass.method1()

根据 Spring Boot 2.1.x 文档, https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1-Release-Notes#logging-refinements

没有 MVC 映射的信息日志。仅将摘要记录为调试日志。 我们必须将级别更新为 TRACE 以获取更多详细信息。

**Spring Boot 2.0.x**
2019-01-24 20:16:08.549 TRACE 2516 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : 
    c.t.c.ControllerClass:
    GET /mapping1: method1()
    GET /mapping2: method2()
2019-01-24 20:16:08.554 TRACE 2516 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : 
    o.s.b.a.w.s.e.BasicErrorController:
     /error: error(HttpServletRequest)
     /error, produces [text/html]: errorHtml(HttpServletRequest,HttpServletResponse)
2019-01-24 20:16:08.560 DEBUG 2516 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : 4 mappings in 'requestMappingHandlerMapping'
2019-01-24 20:16:08.584 DEBUG 2516 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Detected 0 mappings in 'beanNameHandlerMapping'

通过将 Web 日志级别更新为 DEBUG 或 TRACE,我们可以从 Spring Web 获得更多调试日志,但它们并不总是有用。除非,TRACE 级别,它们没有多大意义。

2019-01-24 20:39:59.767 TRACE 2516 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : GET "/mapping1", parameters=, headers=masked in DispatcherServlet 'dispatcherServlet'
2019-01-24 20:39:59.768 TRACE 2516 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public java.lang.String com.test.controller.ControllerClass.method1()
2019-01-24 20:39:59.769 TRACE 2516 --- [nio-8080-exec-5] .w.s.m.m.a.ServletInvocableHandlerMethod : Arguments: []
2019-01-24 20:39:59.771 DEBUG 2516 --- [nio-8080-exec-5] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-01-24 20:39:59.771 TRACE 2516 --- [nio-8080-exec-5] m.m.a.RequestResponseBodyMethodProcessor : Writing ["Method1"]
2019-01-24 20:39:59.776 TRACE 2516 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : No view rendering, null ModelAndView returned.
2019-01-24 20:39:59.777 DEBUG 2516 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Completed 200 OK, headers=masked

是否可以获取 Spring 日志信息?如何 ?

【问题讨论】:

【参考方案1】:

从Spring Boot 2.1版本到控制台获取RequestMappingHandlerMapping日志尝试添加

logging.level.org.springframework.web=trace

application.properties 文件上

它对我有用

【讨论】:

以上是关于Spring Boot 2.1 MVC 日志记录的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot Logging 配置

Spring Boot 日志记录 SLF4J

spring boot slf4j日记记录配置详解

Spring Boot - 日志记录

Spring Boot 日志记录覆盖颜色

Spring Boot 之日志记录