From 7698340201ec9947c06ec5e5de52898a4744bc99 Mon Sep 17 00:00:00 2001 From: seojin Yoon <90759319+7zrv@users.noreply.github.com> Date: Mon, 23 Dec 2024 13:19:26 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=94=84=EB=A1=9C=EB=A9=94=ED=85=8C?= =?UTF-8?q?=EC=9A=B0=EC=8A=A4=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 메트릭 데이터 수집을 위한 프로메테우스 의존성 추가 - 관련 actuator 엔드포인트 설정을 yml에 추가 - log 기록을 위한 logback.xml 추가 - aop를 이용한 로그 기록을 위해 loggingAspect 클래스 추가 --- build.gradle | 9 ++- .../somemore/global/aspect/LoggingAspect.java | 70 +++++++++++++++++++ src/main/resources/application.yml | 11 ++- src/main/resources/logback-spring.xml | 38 ++++++++++ 4 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/somemore/global/aspect/LoggingAspect.java create mode 100644 src/main/resources/logback-spring.xml diff --git a/build.gradle b/build.gradle index 1662f14be..eb5c5d410 100644 --- a/build.gradle +++ b/build.gradle @@ -65,7 +65,10 @@ dependencies { compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.6.0' + + // Monitoring implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '3.3.3' + implementation 'io.micrometer:micrometer-registry-prometheus' //elastic-search implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch' @@ -124,7 +127,8 @@ def jacocoExcludePatterns = [ '**/controller/**', '**/fixture/*', '**/*Factory*', - '**/event/**' + '**/event/**', + '**/*Aspect*' ] def jacocoExcludePatternsForVerify = [ @@ -142,7 +146,8 @@ def jacocoExcludePatternsForVerify = [ '*.controller.*', '*.fixture.*', '*.*Factory*', - '*.*event*.*' + '*.*event*.*', + '*.*Aspect*' ] jacocoTestReport { diff --git a/src/main/java/com/somemore/global/aspect/LoggingAspect.java b/src/main/java/com/somemore/global/aspect/LoggingAspect.java new file mode 100644 index 000000000..ef27d996f --- /dev/null +++ b/src/main/java/com/somemore/global/aspect/LoggingAspect.java @@ -0,0 +1,70 @@ +package com.somemore.global.aspect; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; + +@Slf4j +@Aspect +@Component +public class LoggingAspect { + + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Pointcut("execution(* com.somemore.domains.*.controller..*.*(..))") + private void pointCut(){} + + @Around("pointCut()") + public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { + + String methodName = joinPoint.getSignature().toShortString(); + String args = convertArgsToJson(joinPoint.getArgs()); + log.info("엔드포인트 호출: {} \n- 파라미터: {}", methodName, args); + + long startTime = System.currentTimeMillis(); + + try { + Object result = joinPoint.proceed(); + long elapsedTime = System.currentTimeMillis() - startTime; + + log.debug("성공: {} \n- 응답: {} \n- 실행 시간: {}ms", + methodName, + convertResultToJson(result), + elapsedTime); + + return result; + } catch (Exception e) { + long elapsedTime = System.currentTimeMillis() - startTime; + + log.warn("에러 발생: {} \n- 에러 타입: {} \n- 에러 메세지: {} \n- 실행 시간: {}ms", + methodName, + e.getClass().getSimpleName(), + e.getMessage(), + elapsedTime); + + throw e; + } + } + + private String convertArgsToJson(Object[] args) { + try { + return objectMapper.writeValueAsString(args != null ? args : new Object[]{}); + } catch (Exception e) { + log.warn("파라미터 변환 실패", e); + return "[파라미터 변환 실패]"; + } + } + + private String convertResultToJson(Object result) { + try { + return objectMapper.writeValueAsString(result != null ? result : "null"); + } catch (Exception e) { + log.warn("응답 변환 실패", e); + return "[응답 변환 실패]"; + } + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 68a64f888..79e673d41 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -32,7 +32,7 @@ spring: hibernate: format_sql: true highlight_sql: true - generate_statistics: true + generate_statistics: false show-sql: true data: @@ -125,3 +125,12 @@ management: health: elasticsearch: enabled: false + + endpoints: + web: + exposure: + include: prometheus + + endpoint: + prometheus: + enabled: true diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 000000000..63a0ef2c9 --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,38 @@ + + + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n + UTF-8 + + + + + + 5000 + 0 + + + + ${LOG_DIR:-logs}/application.log + + ${LOG_DIR:-logs}/application.%d{yyyy-MM-dd}.log + 30 + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n + UTF-8 + + + + + + + + + + + + + +