Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -124,7 +127,8 @@ def jacocoExcludePatterns = [
'**/controller/**',
'**/fixture/*',
'**/*Factory*',
'**/event/**'
'**/event/**',
'**/*Aspect*'
]

def jacocoExcludePatternsForVerify = [
Expand All @@ -142,7 +146,8 @@ def jacocoExcludePatternsForVerify = [
'*.controller.*',
'*.fixture.*',
'*.*Factory*',
'*.*event*.*'
'*.*event*.*',
'*.*Aspect*'
]

jacocoTestReport {
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/somemore/global/aspect/LoggingAspect.java
Original file line number Diff line number Diff line change
@@ -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 "[응답 변환 실패]";
}
}
}
11 changes: 10 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ spring:
hibernate:
format_sql: true
highlight_sql: true
generate_statistics: true
generate_statistics: false
show-sql: true

data:
Expand Down Expand Up @@ -125,3 +125,12 @@ management:
health:
elasticsearch:
enabled: false

endpoints:
web:
exposure:
include: prometheus

endpoint:
prometheus:
enabled: true
38 changes: 38 additions & 0 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<configuration>
<springProfile name="default">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>

<appender name="ASYNC_FILE" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="FILE" />
<queueSize>5000</queueSize>
<discardingThreshold>0</discardingThreshold>
</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR:-logs}/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR:-logs}/application.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>

<logger name="com.somemore" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE" />
<appender-ref ref="ASYNC_FILE" />
</logger>

<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="ASYNC_FILE" />
</root>
</springProfile>
</configuration>
Loading