diff --git a/src/main/java/com/splanet/splanet/log/interceptor/ApiLoggingInterceptor.java b/src/main/java/com/splanet/splanet/log/interceptor/ApiLoggingInterceptor.java index 3356291b..1cdcfd44 100644 --- a/src/main/java/com/splanet/splanet/log/interceptor/ApiLoggingInterceptor.java +++ b/src/main/java/com/splanet/splanet/log/interceptor/ApiLoggingInterceptor.java @@ -27,8 +27,11 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons Long userId = (Long) request.getSession().getAttribute("userId"); String deviceId = (String) request.getSession().getAttribute("deviceId"); + // 상태 코드 가져오기 + int statusCode = response.getStatus(); + // 로그 기록 - logService.recordApiRequestLog(userId, deviceId, requestPath, headers); + logService.recordApiRequestLog(userId, deviceId, requestPath, headers, statusCode); return true; } diff --git a/src/main/java/com/splanet/splanet/log/service/LogService.java b/src/main/java/com/splanet/splanet/log/service/LogService.java index bb60c19e..3416a12a 100644 --- a/src/main/java/com/splanet/splanet/log/service/LogService.java +++ b/src/main/java/com/splanet/splanet/log/service/LogService.java @@ -27,10 +27,10 @@ public void recordLoginLog(Long userId, String deviceId, String requestPath, Str } // API 요청 로그 기록 - public void recordApiRequestLog(Long userId, String deviceId, String requestPath, String headers) { + public void recordApiRequestLog(Long userId, String deviceId, String requestPath, String headers, int statusCode) { String timestamp = getCurrentKstTimestamp(); - String logMessage = String.format("eventType: API_REQUEST, userId: %s, deviceId: %s, timestamp: %s, requestPath: %s, headers: %s", - userId, deviceId, timestamp, requestPath, headers); + String logMessage = String.format("eventType: API_REQUEST, userId: %s, deviceId: %s, timestamp: %s, requestPath: %s, headers: %s, statusCode: %d", + userId, deviceId, timestamp, requestPath, headers, statusCode); writeLog(logMessage); } diff --git a/src/main/java/com/splanet/splanet/oauth/OAuth2AuthenticationSuccessHandler.java b/src/main/java/com/splanet/splanet/oauth/OAuth2AuthenticationSuccessHandler.java index 8343ecad..8b9aa67f 100644 --- a/src/main/java/com/splanet/splanet/oauth/OAuth2AuthenticationSuccessHandler.java +++ b/src/main/java/com/splanet/splanet/oauth/OAuth2AuthenticationSuccessHandler.java @@ -98,7 +98,8 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo response.sendRedirect(redirectUrlWithParams); // 토큰 정보는 제외하고 리다이렉트 URL만 로그에 기록 - logService.recordApiRequestLog(user.getId(), deviceId, "Redirected to: " + redirectUrl, headers); + int statusCode = response.getStatus(); // 상태 코드 가져오기 + logService.recordApiRequestLog(user.getId(), deviceId, "Redirected to: " + redirectUrl, headers, statusCode); } catch (IOException e) { logService.recordErrorLog("Failed to redirect after successful authentication", e); } diff --git a/src/test/java/com/splanet/splanet/log/service/LogServiceTest.java b/src/test/java/com/splanet/splanet/log/service/LogServiceTest.java index cb868fb1..d1bc58be 100644 --- a/src/test/java/com/splanet/splanet/log/service/LogServiceTest.java +++ b/src/test/java/com/splanet/splanet/log/service/LogServiceTest.java @@ -64,9 +64,10 @@ void setUp() throws IOException { String deviceId = "apiTestDeviceId"; String requestPath = "/test/api"; String headers = "User-Agent: ApiTestAgent, Accept: application/json"; + int statusCode = 200; // when - logService.recordApiRequestLog(userId, deviceId, requestPath, headers); + logService.recordApiRequestLog(userId, deviceId, requestPath, headers, statusCode); // then File logFile = new File(logPath); @@ -75,7 +76,14 @@ void setUp() throws IOException { // 로그 파일 내용 확인 try { String content = Files.readString(logFile.toPath()); - assertThat(content).contains("eventType: API_REQUEST", "userId: 2", "deviceId: apiTestDeviceId", "requestPath: /test/api", "headers: User-Agent: ApiTestAgent, Accept: application/json"); + assertThat(content).contains( + "eventType: API_REQUEST", + "userId: 2", + "deviceId: apiTestDeviceId", + "requestPath: /test/api", + "headers: User-Agent: ApiTestAgent, Accept: application/json", + "statusCode: 200" + ); } catch (IOException e) { e.printStackTrace(); }