Skip to content

Commit

Permalink
refactor: 상태코드 남기기 (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimsongmok authored Nov 13, 2024
2 parents 791671b + af9f44b commit 8fa9749
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/splanet/splanet/log/service/LogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/com/splanet/splanet/log/service/LogServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down

0 comments on commit 8fa9749

Please sign in to comment.