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 + + + + + + + + + + + + + +