Skip to content

Commit

Permalink
springboot: add request id to log
Browse files Browse the repository at this point in the history
  • Loading branch information
while1618 committed Oct 29, 2024
1 parent 340ccff commit 65cd450
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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;

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 65cd450

Please sign in to comment.