-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
스프링단 로그 추가 #245
Merged
Merged
스프링단 로그 추가 #245
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
67be98b
feat(exception): ExceptionHandler 발생 에러 로깅
jminkkk d3ad484
feat(logger): 요청 쓰레드 구분을 위한 MDC 필터 생성
jminkkk c34ac98
feat(logger): 메서드 이름 및 실행 시간 측정을 위한 Aspect 생성
jminkkk 0d1fb82
feat(logger): 요청 및 응답 로깅용 필터 추가
jminkkk fd4238e
Merge branch 'dev/be' into feat/logging
jminkkk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
backend/src/main/java/codezap/global/logger/MDCFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package codezap.global.logger; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
import jakarta.servlet.Filter; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
|
||
import org.slf4j.MDC; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Component | ||
public class MDCFilter implements Filter { | ||
private final String CORRELATION_ID = "correlationId"; | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
MDC.put(CORRELATION_ID, generateCorrelationId()); | ||
chain.doFilter(request, response); | ||
MDC.clear(); | ||
} | ||
|
||
private String generateCorrelationId() { | ||
return UUID.randomUUID() | ||
.toString() | ||
.substring(0, 8); | ||
jminkkk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
backend/src/main/java/codezap/global/logger/MethodExecutionTimeAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package codezap.global.logger; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Aspect | ||
@Component | ||
public class MethodExecutionTimeAspect { | ||
|
||
@Around("execution(* codezap..*(..)) && " + | ||
"!execution(* codezap.global.logger.MethodExecutionTimeAspect(..))" + | ||
"!execution(* codezap.global.exception.*.*(..))") | ||
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { | ||
if (!log.isInfoEnabled()) { | ||
return joinPoint.proceed(); | ||
} | ||
|
||
long startTime = System.currentTimeMillis(); | ||
Object result = joinPoint.proceed(); | ||
long endTime = System.currentTimeMillis(); | ||
|
||
long executionTimeMillis = endTime - startTime; | ||
|
||
String className = joinPoint.getSignature() | ||
.getDeclaringType() | ||
.getSimpleName(); | ||
String methodName = joinPoint.getSignature() | ||
.getName(); | ||
|
||
log.info("{}.{} 실행 {}ms", className, methodName, executionTimeMillis); | ||
|
||
return result; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
backend/src/main/java/codezap/global/logger/RequestResponseLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package codezap.global.logger; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
import org.springframework.web.util.ContentCachingRequestWrapper; | ||
import org.springframework.web.util.ContentCachingResponseWrapper; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Component | ||
public class RequestResponseLogger extends OncePerRequestFilter { | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request); | ||
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response); | ||
|
||
long startTime = System.currentTimeMillis(); | ||
filterChain.doFilter(requestWrapper, responseWrapper); | ||
long duration = System.currentTimeMillis() - startTime; | ||
|
||
String requestBody = new String(requestWrapper.getContentAsByteArray(), StandardCharsets.UTF_8); | ||
String responseBody = new String(responseWrapper.getContentAsByteArray(), StandardCharsets.UTF_8); | ||
|
||
log.info("[Request] {}, {}, 요청 바디: {}", request.getMethod(), request.getRequestURI(), requestBody); | ||
log.info("[Response] Status: {}, Duration: {}ms, 응답 바디: {}", response.getStatus(), duration, responseBody); | ||
|
||
responseWrapper.copyBodyToResponse(); | ||
} | ||
|
||
@Override | ||
protected boolean shouldNotFilter(HttpServletRequest request) { | ||
String path = request.getRequestURI(); | ||
return path.contains("/swagger") || path.contains("/v3/api-docs"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
우리가 충분히 예상하고, 예외 처리를 해 준 상황이
INFO
레벨의 로그에 기록되는 것이 조금은 어색하게 느껴져요.혹시 아직 로그 레벨에 대한 논의가 진행되지 않았다면, 같이 이야기를 해 보아도 좋을 것 같아요!
꼭 수정해야 하는 것은 아니기에 우선 comment 만 남겨두고 Approve 할게요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
예상했기 때문에 ERROR나 WARN가 아닌 INFO로 설정해두긴 했었어요...!
크리티컬하지는 않지만 사용자에게 400대 응답이 간 것이기 때문에 정보로 남길 필요가 있다고 생각했기 때문에요~
혹시 짱수는 400대 에러에는 어느 레벨이 적절한지 생각하는지 궁금해요!
+) 디스커션으로 얘기 해보면 좋을 것 같아요!