From f070cbe9de19abf9920497f92b545250baa25c12 Mon Sep 17 00:00:00 2001 From: nahyeon99 Date: Sat, 19 Aug 2023 15:47:53 +0900 Subject: [PATCH] =?UTF-8?q?[Refactor]=20logging=20=EB=B0=A9=EC=8B=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 7 +- .../idorm/idormServer/aop/AopLogging.java | 131 ++++++++++++++++++ .../idorm/idormServer/aspect/AopLogging.java | 91 ------------ .../email/service/EmailService.java | 2 +- .../idormServer/exception/ExceptionCode.java | 4 +- src/main/resources/application.yml | 34 ++--- 6 files changed, 157 insertions(+), 112 deletions(-) create mode 100644 src/main/java/idorm/idormServer/aop/AopLogging.java delete mode 100644 src/main/java/idorm/idormServer/aspect/AopLogging.java diff --git a/.gitignore b/.gitignore index 832c79ba..d0d49cfe 100644 --- a/.gitignore +++ b/.gitignore @@ -44,7 +44,10 @@ LOGGING_ROLLING_FILE_PATH_IS_UNDEFINED LOGGING_ERROR_FILE_PATH_IS_UNDEFINED idorm-rolling.log -### Not Useed src/main/**/master -src/main/**/util \ No newline at end of file +src/main/**/util + +src/main/resources/application-local.yml +src/main/resources/application-dev.yml +src/main/resources/application-prod.yml \ No newline at end of file diff --git a/src/main/java/idorm/idormServer/aop/AopLogging.java b/src/main/java/idorm/idormServer/aop/AopLogging.java new file mode 100644 index 00000000..216bf9e6 --- /dev/null +++ b/src/main/java/idorm/idormServer/aop/AopLogging.java @@ -0,0 +1,131 @@ +package idorm.idormServer.aop; + +import idorm.idormServer.exception.CustomException; +import io.sentry.Sentry; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.*; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.stereotype.Component; +import org.springframework.util.StopWatch; + +import java.lang.reflect.Method; + +import static idorm.idormServer.exception.ExceptionCode.*; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; + +@Slf4j +@Aspect +@Component +public class AopLogging { + + @Pointcut("execution(* idorm.idormServer..*Controller.*(..))") + private void methodFromController(){} + + @Pointcut("execution(public * idorm.idormServer..*(..))") + private void all() { + } + + @Before("all()") + public void logBeforeStart(JoinPoint joinPoint) { + + try { + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + + String className = signature.getDeclaringType().getSimpleName(); + Method method = signature.getMethod(); + + String[] parameterNames = signature.getParameterNames(); + Object[] args = joinPoint.getArgs(); + + String returnArgs = null; + + for (int i = 0; i < method.getParameters().length; i++) { + if (parameterNames[i] != null) { + returnArgs += " | " + parameterNames[i] + " = "; + returnArgs += args[i] + " "; + } + } + log.info("[START] {} | {} {}", className, method.getName(), returnArgs); + } catch (Exception e) { + throw new CustomException(e, AOP_LOGGING_ERROR); + } + } + + @AfterReturning(value = "all()", returning = "result") + public void logAfterSuccess(JoinPoint joinPoint, Object result) { + + try { + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + + String className = signature.getDeclaringType().getSimpleName(); + Method method = signature.getMethod(); + + log.info("[SUCCESS] {} | {} | return = {}", className, method.getName(), result); + } catch (Exception e) { + throw new CustomException(e, AOP_LOGGING_ERROR); + } + } + + @Around("methodFromController()") + public Object logEndpoint(ProceedingJoinPoint joinPoint) throws Throwable { + + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + + String className = signature.getDeclaringType().getSimpleName(); + Method method = signature.getMethod(); + + log.info("========= [API START] {} {} API =========", className, method.getName()); + StopWatch stopWatch = new StopWatch(); + + stopWatch.start(); + Object proceed = joinPoint.proceed(); + stopWatch.stop(); + + long totalTimeMillis = stopWatch.getTotalTimeMillis(); + + log.info("========= [API FINISH] {} {} API | {} ms =========", className, method.getName(), totalTimeMillis); + return proceed; + } + + @AfterThrowing(value = "all()", throwing = "exception") + public void logAfterThrowing(JoinPoint joinPoint, CustomException exception) { + + try { + + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + + String className = signature.getDeclaringType().getSimpleName(); + Method method = signature.getMethod(); + + String[] parameterNames = signature.getParameterNames(); + Object[] args = joinPoint.getArgs(); + + String returnArgs = null; + + for (int i = 0; i < method.getParameters().length; i++) { + if (parameterNames[i] != null) { + returnArgs += " | " + parameterNames[i] + " = "; + returnArgs += args[i] + " "; + } + } + + log.error("[ERROR] {} | {} | throwing = {} | reqArgs = {}", + className, + method.getName(), + exception.getExceptionCode().name(), + returnArgs); + + if (exception.getExceptionCode().getHttpStatus().equals(INTERNAL_SERVER_ERROR)) { + + if (exception.getException() != null) { + Sentry.captureException(exception); + log.error("##### SERVER ERROR DESCRIPTION #####", exception.getException()); + } + } + } catch (Exception e) { + throw new CustomException(e, AOP_LOGGING_ERROR); + } + } +} diff --git a/src/main/java/idorm/idormServer/aspect/AopLogging.java b/src/main/java/idorm/idormServer/aspect/AopLogging.java deleted file mode 100644 index efb985a2..00000000 --- a/src/main/java/idorm/idormServer/aspect/AopLogging.java +++ /dev/null @@ -1,91 +0,0 @@ -package idorm.idormServer.aspect; - -import idorm.idormServer.exception.CustomException; -import io.sentry.Sentry; -import lombok.extern.slf4j.Slf4j; -import org.aspectj.lang.JoinPoint; -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.*; -import org.aspectj.lang.reflect.MethodSignature; -import org.springframework.stereotype.Component; -import org.springframework.util.StopWatch; - -import java.lang.reflect.Method; - -import static idorm.idormServer.exception.ExceptionCode.SERVER_ERROR; -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; - -@Slf4j -@Aspect -@Component -public class AopLogging { - - @Pointcut("execution(* idorm.idormServer..*Controller.*(..))") - private void apiTimer(){} - - @Pointcut("execution(public * idorm.idormServer..*Service..*(..))") - private void methodFromService() { - } - - @Around("apiTimer()") - public Object AssumeExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { - - final MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - - final Class className = signature.getDeclaringType(); - final Method method = signature.getMethod(); - - log.info("========= [API START] {} {} API =========", className.getSimpleName(), method.getName()); - StopWatch stopWatch = new StopWatch(); - - stopWatch.start(); - Object proceed = joinPoint.proceed(); - stopWatch.stop(); - - long totalTimeMillis = stopWatch.getTotalTimeMillis(); - - log.info("========= [API FINISH] {} {} API | {} ms =========", - className.getSimpleName(), - method.getName(), - totalTimeMillis); - return proceed; - } - - @AfterThrowing(value = "methodFromService()", throwing = "exception") - public void logAfterThrowingFromService(JoinPoint joinPoint, CustomException exception) { - - try { - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - - Class className = signature.getDeclaringType(); - Method method = signature.getMethod(); - - final String[] parameterNames = signature.getParameterNames(); - final Object[] args = joinPoint.getArgs(); - - String returnArgs = null; - - for (int i = 0; i < method.getParameters().length; i++) { - if (parameterNames[i] != null) { - returnArgs += " | " + parameterNames[i] + " = "; - returnArgs += args[i] + " "; - } - } - - log.error("[ERROR] {} | {} | throwing = {} | reqArgs = {}", - className.getSimpleName(), - method.getName(), - exception.getExceptionCode().name(), - returnArgs); - - if (exception.getExceptionCode().getHttpStatus().equals(INTERNAL_SERVER_ERROR)) { - if (exception.getException() != null) { - Sentry.captureException(exception); - log.error("***** SERVER ERROR DESCRIPTION *****", exception.getException()); - } - } - } catch (Exception e) { - throw new CustomException(e, SERVER_ERROR); - } - } -} diff --git a/src/main/java/idorm/idormServer/email/service/EmailService.java b/src/main/java/idorm/idormServer/email/service/EmailService.java index 9c2f8d5b..05241b31 100644 --- a/src/main/java/idorm/idormServer/email/service/EmailService.java +++ b/src/main/java/idorm/idormServer/email/service/EmailService.java @@ -28,7 +28,7 @@ public class EmailService { private final JavaMailSender javaMailSender; private final EmailRepository emailRepository; - @Value("${MAIL_USERNAME}") + @Value("${spring.mail.username}") private String adminMail; @Value("${s3.logo}") diff --git a/src/main/java/idorm/idormServer/exception/ExceptionCode.java b/src/main/java/idorm/idormServer/exception/ExceptionCode.java index 9c12b964..4e7702eb 100644 --- a/src/main/java/idorm/idormServer/exception/ExceptionCode.java +++ b/src/main/java/idorm/idormServer/exception/ExceptionCode.java @@ -142,7 +142,9 @@ public enum ExceptionCode { SERVER_ERROR(INTERNAL_SERVER_ERROR, "서버 에러가 발생했습니다."), EMAIL_SENDING_ERROR(INTERNAL_SERVER_ERROR, "이메일 전송 중에 서버 에러가 발생했습니다."), FIREBASE_SERER_ERROR(INTERNAL_SERVER_ERROR, "푸시 알람 전송 중에 서버 에러가 발생했습니다."), - S3_SERVER_ERROR(INTERNAL_SERVER_ERROR, "S3 사진 저장 중에 서버 에러가 발생했습니다."); + S3_SERVER_ERROR(INTERNAL_SERVER_ERROR, "S3 사진 저장 중에 서버 에러가 발생했습니다."), + AOP_LOGGING_ERROR(INTERNAL_SERVER_ERROR, "로깅 중 서버 에러가 발생했습니다.") + ; private final HttpStatus httpStatus; private final String message; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ddd3b6b6..5d55c0ec 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -12,18 +12,20 @@ spring: properties: hibernate: show_sql: false - format_sql: false - highlight_sql: false - use_sql_comments: false - - jackson: - serialization: - fail-on-empty-beans: false + format_sql: true + highlight_sql: true + use_sql_comments: true mvc: - pathmatch: + path match: matching-strategy: ant_path_matcher + servlet: + multipart: + max-file-size: ${MAX_FILE_SIZE} + max-request-size: ${MAX_REQUEST_SIZE} + enabled: true + mail: host: ${MAIL_HOST} port: ${MAIL_PORT} @@ -33,15 +35,9 @@ spring: properties: mail: smtp: - auth: ${MAIL_AUTH} + auth: false starttls: - enable: ${MAIL_TLS} - - servlet: - multipart: - max-file-size: ${MAX_FILE_SIZE} - max-request-size: ${MAX_REQUEST_SIZE} - enabled: true + enable: false jwt: secret: ${JWT_SECRET_KEY} @@ -73,4 +69,8 @@ firebase: logging: file: path: ${LOGGING_ROLLING_FILE_PATH} - name: idorm-rolling.log \ No newline at end of file + name: idorm-rolling.log + level: + org: + springframework.web: info + hibernate: info \ No newline at end of file