From dbbdb7d6d82af78a416b517d8b95d5e03cad305e Mon Sep 17 00:00:00 2001 From: kim Date: Wed, 3 Feb 2021 02:50:04 +0900 Subject: [PATCH] aop - logback, sentry --- aop/logs/logback.log | 6 ++++ aop/pom.xml | 4 +-- .../com/example/aop/config/BindingAdvice.java | 13 ++++++-- aop/src/main/resources/application.properties | 1 - aop/src/main/resources/application.yml | 10 ++++++ aop/src/main/resources/logback-spring | 33 +++++++++++++++++++ 6 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 aop/logs/logback.log delete mode 100644 aop/src/main/resources/application.properties create mode 100644 aop/src/main/resources/application.yml create mode 100644 aop/src/main/resources/logback-spring diff --git a/aop/logs/logback.log b/aop/logs/logback.log new file mode 100644 index 00000000..58965cd2 --- /dev/null +++ b/aop/logs/logback.log @@ -0,0 +1,6 @@ +[2021-02-03 02:15:40:12333][http-nio-8080-exec-1] WARN c.example.aop.config.BindingAdvice - com.example.aop.web.UserController.save() => 필드 : username, 메시지 : 유저네임을 입력하세요. +[2021-02-03 02:22:00:7788][http-nio-8080-exec-1] WARN c.example.aop.config.BindingAdvice - com.example.aop.web.UserController.save() => 필드 : username, 메시지 : 유저네임을 입력하세요. +[2021-02-03 02:22:58:7566][http-nio-8080-exec-3] WARN c.example.aop.config.BindingAdvice - com.example.aop.web.UserController.save() => 필드 : username, 메시지 : 유저네임을 입력하세요. +[2021-02-03 02:27:20:17752][http-nio-8080-exec-1] WARN c.example.aop.config.BindingAdvice - com.example.aop.web.UserController.save() => 필드 : username, 메시지 : 유저네임을 입력하세요. +[2021-02-03 02:43:31:15646][http-nio-8080-exec-1] WARN c.example.aop.config.BindingAdvice - com.example.aop.web.UserController.save() => 필드 : username, 메시지 : 유저네임을 입력하세요. +[2021-02-03 02:43:46:30108][http-nio-8080-exec-2] WARN c.example.aop.config.BindingAdvice - com.example.aop.web.UserController.update() => 필드 : username, 메시지 : 패스워드를 입력하세요 diff --git a/aop/pom.xml b/aop/pom.xml index 1f377312..6691ada7 100644 --- a/aop/pom.xml +++ b/aop/pom.xml @@ -51,8 +51,8 @@ io.sentry - sentry - 3.2.1 + sentry-spring-boot-starter + 4.0.0 diff --git a/aop/src/main/java/com/example/aop/config/BindingAdvice.java b/aop/src/main/java/com/example/aop/config/BindingAdvice.java index a192abca..fea77f90 100644 --- a/aop/src/main/java/com/example/aop/config/BindingAdvice.java +++ b/aop/src/main/java/com/example/aop/config/BindingAdvice.java @@ -1,6 +1,8 @@ package com.example.aop.config; import com.example.aop.domain.CommonDto; +import io.sentry.Sentry; +import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; @@ -9,7 +11,6 @@ import org.aspectj.lang.annotation.Before; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.validation.BindingResult; -import org.springframework.validation.FieldError; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -21,8 +22,11 @@ import java.util.Map; // @Configuration -> @Controller , @Service, @Repository -> @Component @Component @Aspect +@Slf4j public class BindingAdvice { +// private static final Logger log = LoggerFactory.getLogger(BindingAdvice.class); + @Before("execution(* com.example.aop.web..*Controller.*(..))") public void before() { HttpServletRequest request = @@ -60,7 +64,12 @@ public class BindingAdvice { .filter(v -> v instanceof BindingResult) .filter(v -> ((BindingResult) v).hasErrors()) .forEach(v -> ((BindingResult) v).getFieldErrors() - .forEach(error -> errorMap.put(error.getField(), error.getDefaultMessage()))); + .forEach(error -> { + errorMap.put(error.getField(), error.getDefaultMessage()); + String msg = type + "." + method + "() => 필드 : " + error.getField() + ", 메시지 : " + error.getDefaultMessage(); + log.warn(msg); + Sentry.captureMessage(msg); + })); return errorMap.isEmpty() ? proceedingJoinPoint.proceed() : new CommonDto<>(errorMap, HttpStatus.BAD_REQUEST.value()); } diff --git a/aop/src/main/resources/application.properties b/aop/src/main/resources/application.properties deleted file mode 100644 index 8b137891..00000000 --- a/aop/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/aop/src/main/resources/application.yml b/aop/src/main/resources/application.yml new file mode 100644 index 00000000..b8084885 --- /dev/null +++ b/aop/src/main/resources/application.yml @@ -0,0 +1,10 @@ +logging: + level: + root: INFO + com.example.aop: WARN + + file: + path: logs + +sentry: + dsn: https://f9bcef6bf8cc45fdb998f145c1ccb763@o515324.ingest.sentry.io/5619969 \ No newline at end of file diff --git a/aop/src/main/resources/logback-spring b/aop/src/main/resources/logback-spring new file mode 100644 index 00000000..be64906d --- /dev/null +++ b/aop/src/main/resources/logback-spring @@ -0,0 +1,33 @@ + + + + + + + [%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %-5level %logger{36} - %msg%n + + + + + ${LOGS_ABSOLUTE_PATH}/logback.log + + [%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %-5level %logger{35} - %msg%n + + + ${LOGS_ABSOLUTE_PATH}/logback.%d{yyyy-MM-dd}.%i.log.gz + + 5MB + + 30 + + + + + + + + + + + + \ No newline at end of file