Skip to content

Commit

Permalink
[merge]: 로깅 로직관련 버그 해결 (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimsongmok authored Nov 15, 2024
2 parents c88dd28 + e28b90f commit b93d16d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package com.splanet.splanet.core.exception;

import com.splanet.splanet.core.exception.ResponseConstants;
import com.splanet.splanet.log.service.LogService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.ConstraintViolationException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;

@RestControllerAdvice
public class GlobalExceptionHandler {
private final LogService logService;

// LogService를 생성자 주입 받습니다.
public GlobalExceptionHandler(LogService logService) {
this.logService = logService;
}

@ExceptionHandler(BusinessException.class)

@ExceptionHandler(BusinessException.class)
protected ResponseEntity<ErrorResponse> handleBusinessException(BusinessException ex) {
String errorMessage = ex.getMessage() != null ? ex.getMessage() : "알 수 없는 오류가 발생했습니다.";
ErrorResponse errorResponse = new ErrorResponse(errorMessage, ex.getErrorCode().getStatus().value());
Expand Down Expand Up @@ -42,4 +56,25 @@ protected ResponseEntity<ErrorResponse> handleDataIntegrityViolationException(Da
ErrorResponse response = new ErrorResponse(ResponseConstants.ErrorMessage.INVALID_INPUT, ResponseConstants.StatusCode.BAD_REQUEST);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(Exception.class)
protected ResponseEntity<ErrorResponse> handleGeneralException(HttpServletRequest request, Exception ex) {
// 예외를 로그로 기록
logService.recordErrorLog("처리되지 않은 예외 발생", ex);

HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
String errorMessage = "서버 내부 오류가 발생했습니다.";

if (ex instanceof MissingServletRequestParameterException) {
status = HttpStatus.BAD_REQUEST;
errorMessage = "필수 파라미터가 누락되었습니다.";
} else if (ex instanceof MethodArgumentNotValidException) {
status = HttpStatus.BAD_REQUEST;
errorMessage = "요청 파라미터가 유효하지 않습니다.";
}
// 필요한 다른 예외 처리 추가

ErrorResponse response = new ErrorResponse(errorMessage, status.value());
return new ResponseEntity<>(response, status);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
return true;
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
Long userId = (Long) request.getSession().getAttribute("userId");
String deviceId = (String) request.getSession().getAttribute("deviceId");
String requestPath = request.getRequestURI();
String headers = getLoggableHeadersAsString(request);
int statusCode = response.getStatus();

// 로그 기록
logService.recordApiRequestLog(userId, deviceId, requestPath, headers, statusCode);
}

private String getLoggableHeadersAsString(HttpServletRequest request) {
StringBuilder headers = new StringBuilder();
loggableHeaders.forEach(headerName -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,13 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
.queryParam("refresh", refreshToken)
.queryParam("deviceId", deviceId)
.build().toUriString();

// 토큰 정보는 제외하고 리다이렉트 URL만 로그에 기록
int statusCode = response.getStatus(); // 상태 코드 가져오기
logService.recordApiRequestLog(user.getId(), deviceId, "Redirected to: " + redirectUrl, headers, statusCode);
// 응답 커밋 상태 확인 후 리다이렉트
if (!response.isCommitted()) {
try {
response.sendRedirect(redirectUrlWithParams);

// 토큰 정보는 제외하고 리다이렉트 URL만 로그에 기록
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

0 comments on commit b93d16d

Please sign in to comment.