【问题标题】:Prevent DispatcherServlet from logging endpoint in Spring Boot Actuator防止 DispatcherServlet 在 Spring Boot Actuator 中记录端点
【发布时间】:2020-05-19 19:24:33
【问题描述】:

我已关闭执行器端点的日志记录,但 DispatcherServlet(和 RequestReponseBodyMethodProcessor)仍会记录映射和响应。

我怎样才能防止只是这个一个端点的这种日志记录?我已经把它们关掉了

management.logging.level.org.springframework.boot.actuate.health=OFF
logging.level.org.springframework.boot.actuate.health=OFF
org.springframework.boot.actuate.health.Logger=OFF

但它仍然是这样的:

2020-05-06 17:14:01.545 DEBUG 58588 --- [nio-9095-exec-5] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/health]
2020-05-06 17:14:01.552 DEBUG 58588 --- [nio-9095-exec-5] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/health] is: -1
2020-05-06 17:14:01.848 DEBUG 58588 --- [nio-9095-exec-5] m.m.a.RequestResponseBodyMethodProcessor : Written [UP {}] as "application/vnd.spring-boot.actuator.v1+json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@144409aa]
2020-05-06 17:14:01.849 DEBUG 58588 --- [nio-9095-exec-5] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2020-05-06 17:14:01.849 DEBUG 58588 --- [nio-9095-exec-5] o.s.web.servlet.DispatcherServlet        : Successfully completed request

【问题讨论】:

    标签: spring logging spring-boot-actuator


    【解决方案1】:

    只需在application.properties日志级别设置为WARN

    logging.level.org.springframework.web.servlet.DispatcherServlet=WARN
    logging.level.org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor=WARN
    

    【讨论】:

      【解决方案2】:

      请检查这是否解决了问题

      在注解 SpringBootApplication 中,在主类中添加排除 DispatcherServletAutoConfiguration 类。

      @SpringBootApplication(exclude = { DispatcherServletAutoConfiguration.class })
      
      

      参考:

      Switch off DispatcherServlet on Spring Boot

      【讨论】:

      • 谢谢。我确实尝试过这个注释,但我所有的端点都没有响应。但主要是,我希望停止从 DispatcherServlet 和 RequestResponseBodyMethodProcessor 的日志记录,只针对一个端点。
      【解决方案3】:

      如果您的目标是记录所有传入的 HTTP 请求,则解决方案是不在 DispatcherServlet 中启用 DEBUG 日志记录级别。

      你可以定义一个bean的类型:

      org.springframework.web.filter.CommonsRequestLoggingFilter

      并重写方法:

      org.springframework.web.filter.CommonsRequestLoggingFilter.shouldLog(HttpServletRequest)

      因此您可以决定哪些请求被记录或不记录。 显然,您必须将过滤器的日志记录级别设置为 DEBUG:

      logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG
      

      更多信息请访问Baeldubg site

      【讨论】:

        猜你喜欢
        • 2016-06-01
        • 2020-09-15
        • 2016-09-07
        • 2021-04-24
        • 2020-03-25
        • 1970-01-01
        • 2020-06-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多