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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/deare/backend/global/aop/LoggingAspect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.deare.backend.global.aop;

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.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class LoggingAspect {

@Around("execution(* com.deare.backend.api..*Service*.*(..))")
public Object logServiceMethod(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String className = joinPoint.getTarget().getClass().getSimpleName();
String methodName = signature.getName();

long start = System.currentTimeMillis();

try {
Object result = joinPoint.proceed();
long elapsed = System.currentTimeMillis() - start;
log.info("[{}] {} - {}ms", className, methodName, elapsed);
return result;
} catch (Exception e) {
long elapsed = System.currentTimeMillis() - start;
log.error("[{}] {} - {}ms - 예외: {}", className, methodName, elapsed, e.getMessage());
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.deare.backend.global.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.MDC;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
import java.util.UUID;

@Component
public class MDCLoggingFilter extends OncePerRequestFilter {

private static final String REQUEST_ID = "requestId";
private static final String USER_ID = "userId";
private static final String METHOD = "method";
private static final String URI = "uri";

@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain
) throws ServletException, IOException {

try {
MDC.put(REQUEST_ID, UUID.randomUUID().toString().substring(0, 8));
MDC.put(METHOD, request.getMethod());
MDC.put(URI, request.getRequestURI());

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null && auth.getPrincipal() instanceof Long userId) {
MDC.put(USER_ID, String.valueOf(userId));
} else {
MDC.put(USER_ID, "anonymous");
}

filterChain.doFilter(request, response);
} finally {
MDC.clear();
}
}
}
11 changes: 10 additions & 1 deletion src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@

<!-- 공통 -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>

<property name="LOG_PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%X{requestId}] [userId=%X{userId}] [%X{method} %X{uri}] %logger{36} - %msg%n"/>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>

<!-- DEV -->
<springProfile name="dev">
Expand Down
Loading