From e6fd4d3ba995588538eac67028e82c6191f46ce1 Mon Sep 17 00:00:00 2001 From: Parkjyun Date: Fri, 12 Jan 2024 21:52:06 +0900 Subject: [PATCH] [feat] create logging for sentry --- build.gradle | 4 ++++ .../exception/GlobalExceptionHandler.java | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index caf7072..3e438f9 100644 --- a/build.gradle +++ b/build.gradle @@ -55,6 +55,10 @@ dependencies { // Social Login implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.1.0' + + // Sentry + implementation 'io.sentry:sentry-spring-boot-starter-jakarta:7.1.0' + implementation 'io.sentry:sentry-logback:7.1.0' } tasks.named('bootBuildImage') { diff --git a/src/main/java/org/pingle/pingleserver/exception/GlobalExceptionHandler.java b/src/main/java/org/pingle/pingleserver/exception/GlobalExceptionHandler.java index 2c205d4..329b769 100644 --- a/src/main/java/org/pingle/pingleserver/exception/GlobalExceptionHandler.java +++ b/src/main/java/org/pingle/pingleserver/exception/GlobalExceptionHandler.java @@ -1,5 +1,6 @@ package org.pingle.pingleserver.exception; +import io.sentry.Sentry; import lombok.extern.slf4j.Slf4j; import org.pingle.pingleserver.dto.common.ApiResponse; import org.pingle.pingleserver.dto.type.ErrorMessage; @@ -21,6 +22,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(value = {MethodArgumentNotValidException.class}) public ResponseEntity> handlerMethodArgumentNotValidException(Exception e) { log.error("handlerMethodArgumentNotValidException() in GlobalExceptionHandler throw MethodArgumentNotValidException : {}", e.getMessage()); + Sentry.captureException(e); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(ApiResponse.fail(ErrorMessage.BAD_REQUEST)); } @@ -28,6 +30,7 @@ public ResponseEntity> handlerMethodArgumentNotValidException(Exc @ExceptionHandler(value={MethodArgumentTypeMismatchException.class}) public ResponseEntity> handlerMethodArgumentTypeMismatchException(Exception e) { log.error("handlerMethodArgumentTypeMismatchException() in GlobalExceptionHandler throw MethodArgumentTypeMismatchException : {}", e.getMessage()); + Sentry.captureException(e); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(ApiResponse.fail(ErrorMessage.BAD_REQUEST)); } @@ -35,6 +38,7 @@ public ResponseEntity> handlerMethodArgumentTypeMismatchException @ExceptionHandler(value = {MissingRequestHeaderException.class}) public ResponseEntity> handlerMissingRequestHeaderException(Exception e) { log.error("handlerMissingRequestHeaderException() in GlobalExceptionHandler throw MissingRequestHeaderException : {}", e.getMessage()); + Sentry.captureException(e); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(ApiResponse.fail(ErrorMessage.MISSING_REQUIRED_HEADER)); } @@ -42,6 +46,7 @@ public ResponseEntity> handlerMissingRequestHeaderException(Excep @ExceptionHandler(HttpMessageNotReadableException.class) public ResponseEntity> handleMessageNotReadableException(HttpMessageNotReadableException e) { log.error("handleMessageNotReadableException() in GlobalExceptionHandler throw HttpMessageNotReadableException : {}", e.getMessage()); + Sentry.captureException(e); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(ApiResponse.fail(ErrorMessage.BAD_REQUEST)); } @@ -49,6 +54,7 @@ public ResponseEntity> handleMessageNotReadableException(HttpMess @ExceptionHandler(value = {NoHandlerFoundException.class}) public ResponseEntity> handleNoPageFoundException(Exception e) { log.error("handleNoPageFoundException() in GlobalExceptionHandler throw NoHandlerFoundException : {}", e.getMessage()); + Sentry.captureException(e); return ResponseEntity.status(HttpStatus.NOT_FOUND) .body(ApiResponse.fail(ErrorMessage.NOT_FOUND_END_POINT)); } @@ -56,13 +62,15 @@ public ResponseEntity> handleNoPageFoundException(Exception e) { @ExceptionHandler(value = {HttpRequestMethodNotSupportedException.class}) public ResponseEntity> handleMethodNotSupportedException(Exception e) { log.error("handleMethodNotSupportedException() in GlobalExceptionHandler throw HttpRequestMethodNotSupportedException : {}", e.getMessage()); + Sentry.captureException(e); return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED) .body(ApiResponse.fail(ErrorMessage.METHOD_NOT_ALLOWED)); } @ExceptionHandler(CustomException.class) public ResponseEntity> handleCustomException(CustomException e) { - log.error("handleException() in GlobalExceptionHandler throw BusinessException : {}", e.getErrorMessage()); + //log.error("handleException() in GlobalExceptionHandler throw BusinessException : {}", e.getErrorMessage()); + Sentry.captureException(e); return ResponseEntity.status(e.getHttpStatusCode()) .body(ApiResponse.fail(e.getErrorMessage())); } @@ -70,6 +78,7 @@ public ResponseEntity> handleCustomException(CustomException e) { @ExceptionHandler(Exception.class) public ResponseEntity> handlerException(Exception e) { log.error("handlerException() in GlobalExceptionHandler throw Exception : {}", e.getMessage()); + Sentry.captureException(e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(ApiResponse.fail(ErrorMessage.INTERNAL_SERVER_ERROR)); }