From 65cd450d25694dbc4ccadf788aff8bc649755380 Mon Sep 17 00:00:00 2001 From: Dejan Zdravkovic Date: Tue, 29 Oct 2024 03:10:59 +0100 Subject: [PATCH] springboot: add request id to log --- .../backend/shared/config/WebMvcConfig.java | 7 ++++++ .../backend/shared/logger/Logger.java | 9 +++++-- .../backend/shared/logger/MDCInterceptor.java | 24 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 backend/spring-boot/src/main/java/org/bootstrapbugz/backend/shared/logger/MDCInterceptor.java diff --git a/backend/spring-boot/src/main/java/org/bootstrapbugz/backend/shared/config/WebMvcConfig.java b/backend/spring-boot/src/main/java/org/bootstrapbugz/backend/shared/config/WebMvcConfig.java index 66935dc7..b01cacfc 100644 --- a/backend/spring-boot/src/main/java/org/bootstrapbugz/backend/shared/config/WebMvcConfig.java +++ b/backend/spring-boot/src/main/java/org/bootstrapbugz/backend/shared/config/WebMvcConfig.java @@ -1,7 +1,9 @@ package org.bootstrapbugz.backend.shared.config; +import org.bootstrapbugz.backend.shared.logger.MDCInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @@ -14,4 +16,9 @@ public void addCorsMappings(CorsRegistry registry) { .allowedMethods("HEAD", "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE") .maxAge(3600); } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new MDCInterceptor()); + } } diff --git a/backend/spring-boot/src/main/java/org/bootstrapbugz/backend/shared/logger/Logger.java b/backend/spring-boot/src/main/java/org/bootstrapbugz/backend/shared/logger/Logger.java index 4465f2f2..887bf765 100644 --- a/backend/spring-boot/src/main/java/org/bootstrapbugz/backend/shared/logger/Logger.java +++ b/backend/spring-boot/src/main/java/org/bootstrapbugz/backend/shared/logger/Logger.java @@ -3,6 +3,7 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.bootstrapbugz.backend.auth.util.AuthUtil; +import org.slf4j.MDC; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -13,7 +14,8 @@ public class Logger { public void info(String message) { final var logDetails = new LogDetails(); log.info( - "USER: {}, IP: {}, REQUEST: {} {}, MESSAGE: {}", + "REQUEST_ID: {}, USER: {}, IP: {}, ENDPOINT: {} {}, MESSAGE: {}", + logDetails.getRequestId(), logDetails.getUsername(), logDetails.getIpAddress(), logDetails.getRequestMethod(), @@ -24,7 +26,8 @@ public void info(String message) { public void error(String message, Exception e) { final var logDetails = new LogDetails(); log.error( - "USER: {}, IP: {}, REQUEST: {} {}, MESSAGE: {}", + "REQUEST_ID: {}, USER: {}, IP: {}, ENDPOINT: {} {}, MESSAGE: {}", + logDetails.getRequestId(), logDetails.getUsername(), logDetails.getIpAddress(), logDetails.getRequestMethod(), @@ -37,6 +40,7 @@ public void error(String message, Exception e) { private static class LogDetails { private final String username; private final String ipAddress; + private final String requestId; private final String requestMethod; private final String requestUrl; @@ -45,6 +49,7 @@ public LogDetails() { ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); username = AuthUtil.getAuthName(); ipAddress = AuthUtil.getUserIpAddress(request); + requestId = MDC.get("REQUEST_ID"); requestMethod = request.getMethod(); requestUrl = request.getRequestURL().toString(); } diff --git a/backend/spring-boot/src/main/java/org/bootstrapbugz/backend/shared/logger/MDCInterceptor.java b/backend/spring-boot/src/main/java/org/bootstrapbugz/backend/shared/logger/MDCInterceptor.java new file mode 100644 index 00000000..7947c76a --- /dev/null +++ b/backend/spring-boot/src/main/java/org/bootstrapbugz/backend/shared/logger/MDCInterceptor.java @@ -0,0 +1,24 @@ +package org.bootstrapbugz.backend.shared.logger; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.util.UUID; +import org.slf4j.MDC; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +@Component +public class MDCInterceptor implements HandlerInterceptor { + @Override + public boolean preHandle( + HttpServletRequest request, HttpServletResponse response, Object handler) { + MDC.put("REQUEST_ID", UUID.randomUUID().toString()); + return true; + } + + @Override + public void afterCompletion( + HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { + MDC.remove("REQUEST_ID"); + } +}