如何在收到请求时和返回响应之前在 Spring 应用程序中记录信息

问题描述 投票:0回答:1

我当前的解决方案提供了过多的日志记录。 Spring有提供一些东西来解决这个问题吗?

场景:

When user request for swagger page (/swagger-ui/index.html)
Then I expect to see only 2 log lines.

预期结果:

2025-01-04T14:48:27.368+02:00 trace-id: 6b4125a6-0072-4c0c-b7eb-dd6bf526f989  INFO 15592 --- [http-nio-8080-exec-1] c.e.spring.logging.HTTPInterceptor                 : -> GET /swagger-ui/index.html 
2025-01-04T14:48:27.401+02:00 trace-id: 6b4125a6-0072-4c0c-b7eb-dd6bf526f989  INFO 15592 --- [http-nio-8080-exec-1] c.e.spring.logging.HTTPInterceptor                 : <- GET [200] /swagger-ui/index.html 

实际结果:

2025-01-04T14:48:27.368+02:00 trace-id: 6b4125a6-0072-4c0c-b7eb-dd6bf526f989  INFO 15592 --- [http-nio-8080-exec-1] c.e.spring.logging.HTTPInterceptor                 : -> GET /swagger-ui/index.html 
2025-01-04T14:48:27.401+02:00 trace-id: 6b4125a6-0072-4c0c-b7eb-dd6bf526f989  INFO 15592 --- [http-nio-8080-exec-1] c.e.spring.logging.HTTPInterceptor                 : <- GET [200] /swagger-ui/index.html 
2025-01-04T14:48:27.415+02:00 trace-id: 964b5360-cb30-4195-9247-c03e0f47077d  INFO 15592 --- [http-nio-8080-exec-5] c.e.spring.logging.HTTPInterceptor                 : -> GET /swagger-ui/swagger-ui-standalone-preset.js 
2025-01-04T14:48:27.415+02:00 trace-id: 09032b5d-cc8e-4368-bbba-e0be97af0d83  INFO 15592 --- [http-nio-8080-exec-2] c.e.spring.logging.HTTPInterceptor                 : -> GET /swagger-ui/swagger-initializer.js 
2025-01-04T14:48:27.415+02:00 trace-id: 4eee41e4-31d7-4b32-95d9-2ce5b47ae680  INFO 15592 --- [http-nio-8080-exec-4] c.e.spring.logging.HTTPInterceptor                 : -> GET /swagger-ui/swagger-ui.css 
2025-01-04T14:48:27.415+02:00 trace-id: c8e18ed2-9ec0-4c20-b924-c829da437d78  INFO 15592 --- [http-nio-8080-exec-6] c.e.spring.logging.HTTPInterceptor                 : -> GET /swagger-ui/swagger-ui-bundle.js 
2025-01-04T14:48:27.415+02:00 trace-id: 5b56c4e4-111a-4859-bfe7-7d8d1e45aa80  INFO 15592 --- [http-nio-8080-exec-3] c.e.spring.logging.HTTPInterceptor                 : -> GET /swagger-ui/index.css 
2025-01-04T14:48:27.420+02:00 trace-id: 5b56c4e4-111a-4859-bfe7-7d8d1e45aa80  INFO 15592 --- [http-nio-8080-exec-3] c.e.spring.logging.HTTPInterceptor                 : <- GET [200] /swagger-ui/index.css 
2025-01-04T14:48:27.421+02:00 trace-id: 4eee41e4-31d7-4b32-95d9-2ce5b47ae680  INFO 15592 --- [http-nio-8080-exec-4] c.e.spring.logging.HTTPInterceptor                 : <- GET [200] /swagger-ui/swagger-ui.css 
2025-01-04T14:48:27.421+02:00 trace-id: 964b5360-cb30-4195-9247-c03e0f47077d  INFO 15592 --- [http-nio-8080-exec-5] c.e.spring.logging.HTTPInterceptor                 : <- GET [200] /swagger-ui/swagger-ui-standalone-preset.js 
2025-01-04T14:48:27.429+02:00 trace-id: c8e18ed2-9ec0-4c20-b924-c829da437d78  INFO 15592 --- [http-nio-8080-exec-6] c.e.spring.logging.HTTPInterceptor                 : <- GET [200] /swagger-ui/swagger-ui-bundle.js 
2025-01-04T14:48:27.444+02:00 trace-id: 09032b5d-cc8e-4368-bbba-e0be97af0d83  INFO 15592 --- [http-nio-8080-exec-2] c.e.spring.logging.HTTPInterceptor                 : <- GET [200] /swagger-ui/swagger-initializer.js 
2025-01-04T14:48:27.563+02:00 trace-id: 9de96e5d-0bff-405b-8175-d30679138cfb  INFO 15592 --- [http-nio-8080-exec-7] c.e.spring.logging.HTTPInterceptor                 : -> GET /swagger-config 
2025-01-04T14:48:27.620+02:00 trace-id: 9de96e5d-0bff-405b-8175-d30679138cfb  INFO 15592 --- [http-nio-8080-exec-7] c.e.spring.logging.HTTPInterceptor                 : <- GET [200] /swagger-config 
2025-01-04T14:48:27.633+02:00 trace-id: a6dc7a18-1b57-4e72-b8c1-02fd9ca62bd8  INFO 15592 --- [http-nio-8080-exec-8] c.e.spring.logging.HTTPInterceptor                 : -> GET /api 
2025-01-04T14:48:27.963+02:00 trace-id: a6dc7a18-1b57-4e72-b8c1-02fd9ca62bd8  INFO 15592 --- [http-nio-8080-exec-8] c.e.spring.logging.HTTPInterceptor                 : <- GET [200] /api

我已经尝试过的解决方案:

  1. OncePerRequestFilter
@Configuration
class HTTPInterceptor : OncePerRequestFilter() {
    override fun doFilterInternal(
        request: HttpServletRequest,
        response: HttpServletResponse,
        filterChain: FilterChain
    ) {
        val traceId = UUID.randomUUID().toString()
        MDC.put(MDCConstants.TRACE_ID, traceId)
        log.info { "-> ${request.method} ${request.requestURI}" }

        filterChain.doFilter(request, response)

        log.info { "<- ${request.method} [${response.status}] ${request.requestURI}" }
        MDC.clear()
    }
}
  1. WebMvcConfigurer
@Configuration
class HTTPInterceptor : WebMvcConfigurer {
        override fun addInterceptors(registry: InterceptorRegistry) {
        registry.addInterceptor(object : HandlerInterceptor {
            override fun preHandle(
                request: HttpServletRequest,
                response: HttpServletResponse,
                handler: Any
            ): Boolean {
                val traceId = UUID.randomUUID().toString()
                MDC.put(TRACE_ID.key, traceId)
                log.info { "-> ${request.method} ${request.requestURI}" }
                return true
            }

            override fun afterCompletion(
                request: HttpServletRequest,
                response: HttpServletResponse,
                handler: Any,
                ex: Exception?
            ) {
                log.info { "<- ${request.method} [${response.status}] ${request.requestURI}" }
                MDC.clear()
            }
        })
    }
}
  1. 我还尝试在
    traceId
    方法的响应标头中添加
    preHandle()
    ,然后检查下一个请求是否在请求标头中包含
    traceId
    ,但由于某种原因它不是持久的。
spring spring-boot kotlin spring-mvc
1个回答
0
投票

Spring AOP 适合这种用例。您可以在方法调用之前、之后或调用时记录日志。那里有很多教程,您可以从Logging With AOP in Spring

开始
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.