From dd8590a243f3f751e21705f271c2a23c0c00d32d Mon Sep 17 00:00:00 2001 From: WonyuChoi Date: Sat, 19 Oct 2024 01:02:43 +0900 Subject: [PATCH 1/8] =?UTF-8?q?feat:=20UserDetails=EC=97=90=20=EB=8B=89?= =?UTF-8?q?=EB=84=A4=EC=9E=84=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../account/auth/application/CustomUserDetailsService.java | 3 ++- .../account/auth/application/dto/CustomUserDetails.java | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/first/flash/account/auth/application/CustomUserDetailsService.java b/src/main/java/com/first/flash/account/auth/application/CustomUserDetailsService.java index c7bc2c61..dd321127 100644 --- a/src/main/java/com/first/flash/account/auth/application/CustomUserDetailsService.java +++ b/src/main/java/com/first/flash/account/auth/application/CustomUserDetailsService.java @@ -19,6 +19,7 @@ public class CustomUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(final String id) throws UsernameNotFoundException { Member foundMember = memberService.findById(UUID.fromString(id)); - return new CustomUserDetails(foundMember.getId(), foundMember.getRole()); + return new CustomUserDetails(foundMember.getId(), foundMember.getRole(), + foundMember.getNickName()); } } diff --git a/src/main/java/com/first/flash/account/auth/application/dto/CustomUserDetails.java b/src/main/java/com/first/flash/account/auth/application/dto/CustomUserDetails.java index 4827fc34..9d6b4446 100644 --- a/src/main/java/com/first/flash/account/auth/application/dto/CustomUserDetails.java +++ b/src/main/java/com/first/flash/account/auth/application/dto/CustomUserDetails.java @@ -8,13 +8,17 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; -public record CustomUserDetails(UUID id, Role role) implements UserDetails { +public record CustomUserDetails(UUID id, Role role, String nickName) implements UserDetails { @Override public Collection getAuthorities() { return Collections.singletonList(new SimpleGrantedAuthority(role.name())); } + public String getNickName() { + return nickName; + } + @Override public String getPassword() { return null; From 89384a44dc7f209570b6cb4d06242fa7bf035cd1 Mon Sep 17 00:00:00 2001 From: WonyuChoi Date: Sat, 19 Oct 2024 01:02:53 +0900 Subject: [PATCH 2/8] =?UTF-8?q?feat:=20=ED=86=B5=ED=95=A9=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=20=EC=B4=88=EC=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flash/global/aspect/LoggingAspect.java | 235 ++++++++++++------ 1 file changed, 162 insertions(+), 73 deletions(-) diff --git a/src/main/java/com/first/flash/global/aspect/LoggingAspect.java b/src/main/java/com/first/flash/global/aspect/LoggingAspect.java index de7ec490..e568164f 100644 --- a/src/main/java/com/first/flash/global/aspect/LoggingAspect.java +++ b/src/main/java/com/first/flash/global/aspect/LoggingAspect.java @@ -1,14 +1,16 @@ package com.first.flash.global.aspect; -import com.first.flash.global.util.AuthUtil; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.first.flash.account.auth.application.dto.CustomUserDetails; import jakarta.servlet.http.HttpServletRequest; import java.lang.reflect.Method; -import java.util.Enumeration; -import java.util.Objects; +import java.util.HashMap; +import java.util.Map; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; +import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.reflect.MethodSignature; @@ -23,92 +25,179 @@ @RequiredArgsConstructor public class LoggingAspect { - @Before("com.first.flash.global.aspect.PointCuts.allController()") - public void doBeforeController() { - HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()) - .getRequest(); - - log.info("======= Controller Start ======="); - log.info("Request URI: {}", request.getRequestURI()); - log.info("HTTP Method: {}", request.getMethod()); - - Enumeration headerNames = request.getHeaderNames(); - while (headerNames.hasMoreElements()) { - String headerName = headerNames.nextElement(); - log.info("Header: {} = {}", headerName, request.getHeader(headerName)); - } + private final ObjectMapper objectMapper = new ObjectMapper(); + private final ThreadLocal> logContext = ThreadLocal.withInitial( + HashMap::new); - Enumeration parameterNames = request.getParameterNames(); - while (parameterNames.hasMoreElements()) { - String parameterName = parameterNames.nextElement(); - log.info("Parameter: {} = {}", parameterName, request.getParameter(parameterName)); - } + @AfterReturning(pointcut = "execution(* com.first.flash.account.auth.application.CustomUserDetailsService.loadUserByUsername(..))", returning = "userDetails") + public void logUserDetails(final CustomUserDetails userDetails) { + Map logData = logContext.get(); + logData.put("Requester", userDetails.getNickName()); + logContext.set(logData); } - @Before("com.first.flash.global.aspect.PointCuts.verifiedControllers()") - public void doBeforeVerifiedController() { - log.info("user id: {}", AuthUtil.getId()); + @Before("com.first.flash.global.aspect.PointCuts.allController()") + public void logRequestInfo(final JoinPoint joinPoint) { + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); + + Map logData = logContext.get(); + logData.put("request URI", request.getRequestURI()); + logData.put("HTTP method", request.getMethod()); + logContext.set(logData); } - @AfterReturning(value = "com.first.flash.global.aspect.PointCuts.allController()", returning = "result") - public void afterReturnController(final Object result) { - if (result instanceof ResponseEntity responseEntity) { - int statusCode = responseEntity.getStatusCode().value(); - Object responseBody = responseEntity.getBody(); + @Before("com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()") + public void logMethodDetails(final JoinPoint joinPoint) { + if (log.isDebugEnabled()) { + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = signature.getMethod(); + String className = joinPoint.getTarget().getClass().getSimpleName(); - log.info("Response Status Code: {}", statusCode); - if (!Objects.isNull(responseBody)) { - log.info("Response Body: {}", responseBody); - } else { - log.info("no Response Body"); - } - } else { - if (!Objects.isNull(result)) { - log.info("Response: {}", result); + Map logData = logContext.get(); + logData.put(className + " Method", method.getName()); + + Object[] args = joinPoint.getArgs(); + if (args.length > 0) { + logData.put("Method Parameters", args); } else { - log.info("no Response"); + logData.put("Method Parameters", "No parameters"); } + logContext.set(logData); } - log.info("======= Controller End ======="); } - @Before("com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()") - public void doBefore(final JoinPoint joinPoint) { - Method method = getMethod(joinPoint); - String objectName = joinPoint.getTarget() - .getClass() - .getSimpleName(); - log.info("======= {} Start =======", objectName); - log.info("method name = {}", method.getName()); - Object[] args = joinPoint.getArgs(); - if (Objects.isNull(args) || args.length == 0) { - log.info("no parameter"); - return; - } - for (Object arg : args) { - if (Objects.isNull(arg)) { - continue; - } - log.info("parameter type = {}", arg.getClass().getSimpleName()); - log.info("parameter value = {}", arg); + @AfterReturning(pointcut = "com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()", returning = "result") + public void logMethodReturn(final JoinPoint joinPoint, final Object result) { + if (log.isDebugEnabled()) { + Map logData = logContext.get(); + logData.put("Method Return Value", result); + logContext.set(logData); } } - @AfterReturning(value = "com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()", returning = "result") - public void afterReturnLog(final JoinPoint joinPoint, final Object result) { - if (Objects.isNull(result)) { - return; + @AfterReturning(pointcut = "com.first.flash.global.aspect.PointCuts.allController()", returning = "result") + public void logResponse(final Object result) { + Map logData = logContext.get(); + + if (result instanceof ResponseEntity responseEntity) { + logData.put("Response Status", responseEntity.getStatusCode().value()); + logData.put("Response Body", responseEntity.getBody()); + } else { + logData.put("Response", result); + } + + try { + log.info("Unified Log: {}", objectMapper.writeValueAsString(logData)); + } catch (Exception e) { + log.error("Error converting log to JSON", e); + } finally { + logContext.remove(); } - log.info("return type = {}", result.getClass().getSimpleName()); - log.info("return value = {}", result); - String objectName = joinPoint.getTarget() - .getClass() - .getSimpleName(); - log.info("======= {} End =======", objectName); } - private Method getMethod(final JoinPoint joinPoint) { - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - return signature.getMethod(); + // ExceptionHandler에서 예외 기록 + @AfterThrowing(pointcut = "execution(* com.first.flash..*ExceptionHandler.*(..))", throwing = "exception") + public void logException(RuntimeException exception) { + Map logData = logContext.get(); + + logData.put("Error Class", exception.getClass().getSimpleName()); + logData.put("Error Message", exception.getMessage()); + + try { + log.error("Unified Log with Exception: {}", objectMapper.writeValueAsString(logData)); + } catch (Exception e) { + log.error("Error converting log with exception to JSON", e); + } finally { + logContext.remove(); + } } + +// @Before("com.first.flash.global.aspect.PointCuts.allController()") +// public void doBeforeController() { +// HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()) +// .getRequest(); +// +// log.info("======= Controller Start ======="); +// log.info("Request URI: {}", request.getRequestURI()); +// log.info("HTTP Method: {}", request.getMethod()); +// +// Enumeration headerNames = request.getHeaderNames(); +// while (headerNames.hasMoreElements()) { +// String headerName = headerNames.nextElement(); +// log.info("Header: {} = {}", headerName, request.getHeader(headerName)); +// } +// +// Enumeration parameterNames = request.getParameterNames(); +// while (parameterNames.hasMoreElements()) { +// String parameterName = parameterNames.nextElement(); +// log.info("Parameter: {} = {}", parameterName, request.getParameter(parameterName)); +// } +// } +// +// @Before("com.first.flash.global.aspect.PointCuts.verifiedControllers()") +// public void doBeforeVerifiedController() { +// log.info("user id: {}", AuthUtil.getId()); +// } +// +// @AfterReturning(value = "com.first.flash.global.aspect.PointCuts.allController()", returning = "result") +// public void afterReturnController(final Object result) { +// if (result instanceof ResponseEntity responseEntity) { +// int statusCode = responseEntity.getStatusCode().value(); +// Object responseBody = responseEntity.getBody(); +// +// log.info("Response Status Code: {}", statusCode); +// if (!Objects.isNull(responseBody)) { +// log.info("Response Body: {}", responseBody); +// } else { +// log.info("no Response Body"); +// } +// } else { +// if (!Objects.isNull(result)) { +// log.info("Response: {}", result); +// } else { +// log.info("no Response"); +// } +// } +// log.info("======= Controller End ======="); +// } +// +// @Before("com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()") +// public void doBefore(final JoinPoint joinPoint) { +// Method method = getMethod(joinPoint); +// String objectName = joinPoint.getTarget() +// .getClass() +// .getSimpleName(); +// log.info("======= {} Start =======", objectName); +// log.info("method name = {}", method.getName()); +// Object[] args = joinPoint.getArgs(); +// if (Objects.isNull(args) || args.length == 0) { +// log.info("no parameter"); +// return; +// } +// for (Object arg : args) { +// if (Objects.isNull(arg)) { +// continue; +// } +// log.info("parameter type = {}", arg.getClass().getSimpleName()); +// log.info("parameter value = {}", arg); +// } +// } +// +// @AfterReturning(value = "com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()", returning = "result") +// public void afterReturnLog(final JoinPoint joinPoint, final Object result) { +// if (Objects.isNull(result)) { +// return; +// } +// log.info("return type = {}", result.getClass().getSimpleName()); +// log.info("return value = {}", result); +// String objectName = joinPoint.getTarget() +// .getClass() +// .getSimpleName(); +// log.info("======= {} End =======", objectName); +// } +// +// private Method getMethod(final JoinPoint joinPoint) { +// MethodSignature signature = (MethodSignature) joinPoint.getSignature(); +// return signature.getMethod(); +// } } From 26f6fbfe150086f308d0514cd417acce975b9d32 Mon Sep 17 00:00:00 2001 From: WonyuChoi Date: Sun, 20 Oct 2024 11:18:41 +0900 Subject: [PATCH 3/8] =?UTF-8?q?feat:=20=ED=82=A4=EA=B0=92=20=EC=86=8C?= =?UTF-8?q?=EB=AC=B8=EC=9E=90=EB=A1=9C=20=ED=86=B5=EC=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flash/global/aspect/LoggingAspect.java | 118 +++--------------- 1 file changed, 15 insertions(+), 103 deletions(-) diff --git a/src/main/java/com/first/flash/global/aspect/LoggingAspect.java b/src/main/java/com/first/flash/global/aspect/LoggingAspect.java index e568164f..b01e1468 100644 --- a/src/main/java/com/first/flash/global/aspect/LoggingAspect.java +++ b/src/main/java/com/first/flash/global/aspect/LoggingAspect.java @@ -32,7 +32,7 @@ public class LoggingAspect { @AfterReturning(pointcut = "execution(* com.first.flash.account.auth.application.CustomUserDetailsService.loadUserByUsername(..))", returning = "userDetails") public void logUserDetails(final CustomUserDetails userDetails) { Map logData = logContext.get(); - logData.put("Requester", userDetails.getNickName()); + logData.put("requester", userDetails.getNickName()); logContext.set(logData); } @@ -54,13 +54,13 @@ public void logMethodDetails(final JoinPoint joinPoint) { String className = joinPoint.getTarget().getClass().getSimpleName(); Map logData = logContext.get(); - logData.put(className + " Method", method.getName()); + logData.put(className + " method", method.getName()); Object[] args = joinPoint.getArgs(); if (args.length > 0) { - logData.put("Method Parameters", args); + logData.put("method param", args); } else { - logData.put("Method Parameters", "No parameters"); + logData.put("method params", "no params"); } logContext.set(logData); } @@ -70,7 +70,7 @@ public void logMethodDetails(final JoinPoint joinPoint) { public void logMethodReturn(final JoinPoint joinPoint, final Object result) { if (log.isDebugEnabled()) { Map logData = logContext.get(); - logData.put("Method Return Value", result); + logData.put("method return value", result); logContext.set(logData); } } @@ -78,16 +78,17 @@ public void logMethodReturn(final JoinPoint joinPoint, final Object result) { @AfterReturning(pointcut = "com.first.flash.global.aspect.PointCuts.allController()", returning = "result") public void logResponse(final Object result) { Map logData = logContext.get(); + logData.put("type", "log"); if (result instanceof ResponseEntity responseEntity) { - logData.put("Response Status", responseEntity.getStatusCode().value()); - logData.put("Response Body", responseEntity.getBody()); + logData.put("response status", responseEntity.getStatusCode().value()); + logData.put("response body", responseEntity.getBody()); } else { - logData.put("Response", result); + logData.put("response", result); } try { - log.info("Unified Log: {}", objectMapper.writeValueAsString(logData)); + log.info(objectMapper.writeValueAsString(logData)); } catch (Exception e) { log.error("Error converting log to JSON", e); } finally { @@ -95,109 +96,20 @@ public void logResponse(final Object result) { } } - // ExceptionHandler에서 예외 기록 @AfterThrowing(pointcut = "execution(* com.first.flash..*ExceptionHandler.*(..))", throwing = "exception") - public void logException(RuntimeException exception) { + public void logException(final RuntimeException exception) { Map logData = logContext.get(); - logData.put("Error Class", exception.getClass().getSimpleName()); - logData.put("Error Message", exception.getMessage()); + logData.put("type", "error"); + logData.put("error class", exception.getClass().getSimpleName()); + logData.put("error message", exception.getMessage()); try { - log.error("Unified Log with Exception: {}", objectMapper.writeValueAsString(logData)); + log.error(objectMapper.writeValueAsString(logData)); } catch (Exception e) { log.error("Error converting log with exception to JSON", e); } finally { logContext.remove(); } } - -// @Before("com.first.flash.global.aspect.PointCuts.allController()") -// public void doBeforeController() { -// HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()) -// .getRequest(); -// -// log.info("======= Controller Start ======="); -// log.info("Request URI: {}", request.getRequestURI()); -// log.info("HTTP Method: {}", request.getMethod()); -// -// Enumeration headerNames = request.getHeaderNames(); -// while (headerNames.hasMoreElements()) { -// String headerName = headerNames.nextElement(); -// log.info("Header: {} = {}", headerName, request.getHeader(headerName)); -// } -// -// Enumeration parameterNames = request.getParameterNames(); -// while (parameterNames.hasMoreElements()) { -// String parameterName = parameterNames.nextElement(); -// log.info("Parameter: {} = {}", parameterName, request.getParameter(parameterName)); -// } -// } -// -// @Before("com.first.flash.global.aspect.PointCuts.verifiedControllers()") -// public void doBeforeVerifiedController() { -// log.info("user id: {}", AuthUtil.getId()); -// } -// -// @AfterReturning(value = "com.first.flash.global.aspect.PointCuts.allController()", returning = "result") -// public void afterReturnController(final Object result) { -// if (result instanceof ResponseEntity responseEntity) { -// int statusCode = responseEntity.getStatusCode().value(); -// Object responseBody = responseEntity.getBody(); -// -// log.info("Response Status Code: {}", statusCode); -// if (!Objects.isNull(responseBody)) { -// log.info("Response Body: {}", responseBody); -// } else { -// log.info("no Response Body"); -// } -// } else { -// if (!Objects.isNull(result)) { -// log.info("Response: {}", result); -// } else { -// log.info("no Response"); -// } -// } -// log.info("======= Controller End ======="); -// } -// -// @Before("com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()") -// public void doBefore(final JoinPoint joinPoint) { -// Method method = getMethod(joinPoint); -// String objectName = joinPoint.getTarget() -// .getClass() -// .getSimpleName(); -// log.info("======= {} Start =======", objectName); -// log.info("method name = {}", method.getName()); -// Object[] args = joinPoint.getArgs(); -// if (Objects.isNull(args) || args.length == 0) { -// log.info("no parameter"); -// return; -// } -// for (Object arg : args) { -// if (Objects.isNull(arg)) { -// continue; -// } -// log.info("parameter type = {}", arg.getClass().getSimpleName()); -// log.info("parameter value = {}", arg); -// } -// } -// -// @AfterReturning(value = "com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()", returning = "result") -// public void afterReturnLog(final JoinPoint joinPoint, final Object result) { -// if (Objects.isNull(result)) { -// return; -// } -// log.info("return type = {}", result.getClass().getSimpleName()); -// log.info("return value = {}", result); -// String objectName = joinPoint.getTarget() -// .getClass() -// .getSimpleName(); -// log.info("======= {} End =======", objectName); -// } -// -// private Method getMethod(final JoinPoint joinPoint) { -// MethodSignature signature = (MethodSignature) joinPoint.getSignature(); -// return signature.getMethod(); -// } } From 43374022ff4614d19a12626d0826aa17459c40df Mon Sep 17 00:00:00 2001 From: WonyuChoi Date: Sun, 20 Oct 2024 11:19:04 +0900 Subject: [PATCH 4/8] =?UTF-8?q?setting:=20LocalDate=20=EC=A7=81=EB=A0=AC?= =?UTF-8?q?=ED=99=94=EB=A5=BC=20=EC=9C=84=ED=95=9C=20=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.gradle b/build.gradle index 307a03f4..81b79449 100644 --- a/build.gradle +++ b/build.gradle @@ -61,6 +61,8 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-security' testImplementation 'org.springframework.security:spring-security-test' + // json + implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.17.1' } tasks.named('test') { From f5c295e3da367728d084533cdacad63e79b3d790 Mon Sep 17 00:00:00 2001 From: WonyuChoi Date: Sun, 20 Oct 2024 11:19:43 +0900 Subject: [PATCH 5/8] =?UTF-8?q?feat:=20Object=20Mapper=EC=9D=98=20?= =?UTF-8?q?=EC=8B=9C=EA=B0=84=20=EC=A7=81=EB=A0=AC=ED=99=94=20=EB=AA=A8?= =?UTF-8?q?=EB=93=88=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/first/flash/global/aspect/LoggingAspect.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/com/first/flash/global/aspect/LoggingAspect.java b/src/main/java/com/first/flash/global/aspect/LoggingAspect.java index b01e1468..9986e97a 100644 --- a/src/main/java/com/first/flash/global/aspect/LoggingAspect.java +++ b/src/main/java/com/first/flash/global/aspect/LoggingAspect.java @@ -1,7 +1,10 @@ package com.first.flash.global.aspect; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.first.flash.account.auth.application.dto.CustomUserDetails; +import jakarta.annotation.PostConstruct; import jakarta.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.util.HashMap; @@ -29,6 +32,12 @@ public class LoggingAspect { private final ThreadLocal> logContext = ThreadLocal.withInitial( HashMap::new); + @PostConstruct + public void setUp() { + objectMapper.registerModule(new JavaTimeModule()); + objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // ISO-8601 형식으로 출력 + } + @AfterReturning(pointcut = "execution(* com.first.flash.account.auth.application.CustomUserDetailsService.loadUserByUsername(..))", returning = "userDetails") public void logUserDetails(final CustomUserDetails userDetails) { Map logData = logContext.get(); From d88c326fe0f5a092636d97de6581c99ada867a23 Mon Sep 17 00:00:00 2001 From: WonyuChoi Date: Sun, 20 Oct 2024 14:11:13 +0900 Subject: [PATCH 6/8] =?UTF-8?q?feat:=20=EC=97=90=EB=9F=AC=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=20=ED=95=B8=EB=93=A4=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flash/global/aspect/LoggingAspect.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/first/flash/global/aspect/LoggingAspect.java b/src/main/java/com/first/flash/global/aspect/LoggingAspect.java index 9986e97a..1ae2a063 100644 --- a/src/main/java/com/first/flash/global/aspect/LoggingAspect.java +++ b/src/main/java/com/first/flash/global/aspect/LoggingAspect.java @@ -13,7 +13,6 @@ import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; -import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.reflect.MethodSignature; @@ -105,18 +104,22 @@ public void logResponse(final Object result) { } } - @AfterThrowing(pointcut = "execution(* com.first.flash..*ExceptionHandler.*(..))", throwing = "exception") - public void logException(final RuntimeException exception) { + @AfterReturning(pointcut = "execution(* com.first.flash..*ExceptionHandler.*(..))", returning = "exception") + public void logException(final Object exception) { Map logData = logContext.get(); - logData.put("type", "error"); - logData.put("error class", exception.getClass().getSimpleName()); - logData.put("error message", exception.getMessage()); + + if (exception instanceof ResponseEntity exceptionResponse) { + logData.put("error status", exceptionResponse.getStatusCode().value()); + logData.put("error body", exceptionResponse.getBody()); + } else { + logData.put("error", exception); + } try { - log.error(objectMapper.writeValueAsString(logData)); + log.info(objectMapper.writeValueAsString(logData)); } catch (Exception e) { - log.error("Error converting log with exception to JSON", e); + log.error("Error converting log to JSON", e); } finally { logContext.remove(); } From bcb98a6b7f367a931e5bf4a1c516dc299fc655ca Mon Sep 17 00:00:00 2001 From: WonyuChoi Date: Sun, 20 Oct 2024 14:17:39 +0900 Subject: [PATCH 7/8] =?UTF-8?q?refactor:=20exception=20handler=EB=A5=BC=20?= =?UTF-8?q?=ED=8F=AC=EC=9D=B8=ED=8A=B8=EC=BB=B7=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/first/flash/global/aspect/LoggingAspect.java | 2 +- src/main/java/com/first/flash/global/aspect/PointCuts.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/first/flash/global/aspect/LoggingAspect.java b/src/main/java/com/first/flash/global/aspect/LoggingAspect.java index 1ae2a063..b8e59a03 100644 --- a/src/main/java/com/first/flash/global/aspect/LoggingAspect.java +++ b/src/main/java/com/first/flash/global/aspect/LoggingAspect.java @@ -104,7 +104,7 @@ public void logResponse(final Object result) { } } - @AfterReturning(pointcut = "execution(* com.first.flash..*ExceptionHandler.*(..))", returning = "exception") + @AfterReturning(pointcut = "com.first.flash.global.aspect.PointCuts.allExceptionHandler()", returning = "exception") public void logException(final Object exception) { Map logData = logContext.get(); logData.put("type", "error"); diff --git a/src/main/java/com/first/flash/global/aspect/PointCuts.java b/src/main/java/com/first/flash/global/aspect/PointCuts.java index a3ee2ae1..148fe6f3 100644 --- a/src/main/java/com/first/flash/global/aspect/PointCuts.java +++ b/src/main/java/com/first/flash/global/aspect/PointCuts.java @@ -16,8 +16,11 @@ public void allService() { public void allRepository() { } + @Pointcut("execution(* com.first.flash..*ExceptionHandler.*(..))") + public void allExceptionHandler() { + } + @Pointcut("execution(* com.first.flash.climbing..*Controller.*(..))") public void verifiedControllers() { - } } From 0c233a367c5885b166e653c5f32f7495157960ef Mon Sep 17 00:00:00 2001 From: WonyuChoi Date: Sun, 20 Oct 2024 14:54:50 +0900 Subject: [PATCH 8/8] =?UTF-8?q?feat:=20debug=20=EB=A0=88=EB=B2=A8=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=20=EC=A3=BC=EC=84=9D=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flash/global/aspect/LoggingAspect.java | 79 ++++++++++++------- 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/first/flash/global/aspect/LoggingAspect.java b/src/main/java/com/first/flash/global/aspect/LoggingAspect.java index b8e59a03..078da0cf 100644 --- a/src/main/java/com/first/flash/global/aspect/LoggingAspect.java +++ b/src/main/java/com/first/flash/global/aspect/LoggingAspect.java @@ -6,7 +6,6 @@ import com.first.flash.account.auth.application.dto.CustomUserDetails; import jakarta.annotation.PostConstruct; import jakarta.servlet.http.HttpServletRequest; -import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import lombok.RequiredArgsConstructor; @@ -15,7 +14,6 @@ import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; -import org.aspectj.lang.reflect.MethodSignature; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; @@ -54,34 +52,34 @@ public void logRequestInfo(final JoinPoint joinPoint) { logContext.set(logData); } - @Before("com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()") - public void logMethodDetails(final JoinPoint joinPoint) { - if (log.isDebugEnabled()) { - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - Method method = signature.getMethod(); - String className = joinPoint.getTarget().getClass().getSimpleName(); - - Map logData = logContext.get(); - logData.put(className + " method", method.getName()); - - Object[] args = joinPoint.getArgs(); - if (args.length > 0) { - logData.put("method param", args); - } else { - logData.put("method params", "no params"); - } - logContext.set(logData); - } - } - - @AfterReturning(pointcut = "com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()", returning = "result") - public void logMethodReturn(final JoinPoint joinPoint, final Object result) { - if (log.isDebugEnabled()) { - Map logData = logContext.get(); - logData.put("method return value", result); - logContext.set(logData); - } - } +// @Before("com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()") +// public void logMethodDetails(final JoinPoint joinPoint) { +// if (log.isDebugEnabled()) { +// String key = generateLogKey(joinPoint); +// +// Map methodDetails = new HashMap<>(); +// Object[] args = joinPoint.getArgs(); +// if (args.length > 0) { +// methodDetails.put("params", args); +// } else { +// methodDetails.put("params", "no params"); +// } +// +// updateLogContext(key, methodDetails); +// } +// } +// +// @AfterReturning(pointcut = "com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()", returning = "result") +// public void logMethodReturn(final JoinPoint joinPoint, final Object result) { +// if (log.isDebugEnabled()) { +// String key = generateLogKey(joinPoint); +// +// Map methodDetails = new HashMap<>(); +// methodDetails.put("returnValue", result); +// +// updateLogContext(key, methodDetails); +// } +// } @AfterReturning(pointcut = "com.first.flash.global.aspect.PointCuts.allController()", returning = "result") public void logResponse(final Object result) { @@ -124,4 +122,25 @@ public void logException(final Object exception) { logContext.remove(); } } + +// private String generateLogKey(final JoinPoint joinPoint) { +// MethodSignature signature = (MethodSignature) joinPoint.getSignature(); +// String className = joinPoint.getTarget().getClass().getSimpleName(); +// String methodName = signature.getMethod().getName(); +// return className + "." + methodName; +// } +// +// private void updateLogContext(final String key, final Map methodDetails) { +// Map logData = logContext.get(); +// +// if (logData.containsKey(key)) { +// Map existingDetails = (Map) logData.get(key); +// existingDetails.putAll(methodDetails); +// logData.put(key, existingDetails); +// } else { +// logData.put(key, methodDetails); +// } +// +// logContext.set(logData); +// } }