-
Notifications
You must be signed in to change notification settings - Fork 3
[Feat] MDC 로깅 구현 #121
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
Merged
Merged
[Feat] MDC 로깅 구현 #121
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8c36f59
[Feat] #119 MDC 로깅 필터 구현
ksg1227 aebf637
[Feat] #119 필터 체인에 로깅 필터 등록
ksg1227 05438d7
[Feat] #119 jwtFilter 를 거칠 때, MDC에 userId를 담아주도록 구현 및 logback-spring.…
ksg1227 7bcd34c
[Feat] #119 환경 별 로그 레벨 설정
ksg1227 66ab95c
[Fix] #119 로그 레벨을 info 로 설정
ksg1227 0ec7a8a
[Fix] #119 MDC 비우는 위치 수정
ksg1227 49051f9
[Feat] #119 github에 로그파일이 올라가지 않도록 ignore 처리
ksg1227 0054786
[Feat] #119 ec2 호스트와 도커 컨테이너간의 볼륨 마운트 설정
ksg1227 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 hidden or 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 |
|---|---|---|
|
|
@@ -38,3 +38,6 @@ out/ | |
|
|
||
| ### Mac OS ### | ||
| .DS_Store | ||
|
|
||
| ### log ### | ||
| logs/ | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/kuit/findyou/global/logging/MDCLoggingFilter.java
This file contains hidden or 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,30 @@ | ||
| package com.kuit.findyou.global.logging; | ||
|
|
||
| import jakarta.servlet.*; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import org.slf4j.MDC; | ||
| import org.springframework.core.Ordered; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
| import java.util.UUID; | ||
|
|
||
|
|
||
| @Component | ||
| @Order(Ordered.HIGHEST_PRECEDENCE) | ||
| public class MDCLoggingFilter implements Filter { | ||
|
|
||
| @Override | ||
| public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | ||
| try { | ||
| String requestId = ((HttpServletRequest)servletRequest).getHeader("X-Request-ID"); | ||
| MDC.put("request_id", Objects.toString(requestId, UUID.randomUUID().toString())); | ||
|
|
||
| filterChain.doFilter(servletRequest, servletResponse); | ||
| } finally { | ||
| MDC.clear(); // 메모리 누수 방지 | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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,85 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <configuration> | ||
| <!-- 색상 변환기 등록(콘솔용) --> | ||
| <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" /> | ||
| <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /> | ||
| <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" /> | ||
|
|
||
| <property name="LOG_PATH" value="logs"/> | ||
|
|
||
| <!-- 콘솔 로그 패턴(컬러) --> | ||
| <property name="console.format" | ||
| value="[%clr(%d{yyyy-MM-dd HH:mm:ss}){green}:%clr(%-3relative){faint}] [%clr(%thread){magenta}] %highlight(%-5level) %clr(%logger{35}){cyan} - %clr(%msg){yellow}%n"/> | ||
|
|
||
| <!-- 파일 로그 패턴 (텍스트, key=value 형태) --> | ||
| <property name="file.format" | ||
| value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%level] [%thread] [%logger] [request_id:%X{request_id}] [user_id:%X{user_id}] %msg%n"/> | ||
|
|
||
| <!-- 콘솔 로그 Appender(컬러) --> | ||
| <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
| <encoder> | ||
| <pattern>${console.format}</pattern> | ||
| </encoder> | ||
| </appender> | ||
|
|
||
| <!-- INFO 로그 파일 (텍스트) --> | ||
| <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
| <file>${LOG_PATH}/info.log</file> | ||
| <encoder> | ||
| <pattern>${file.format}</pattern> | ||
| </encoder> | ||
| <filter class="ch.qos.logback.classic.filter.LevelFilter"> | ||
| <level>INFO</level> | ||
| <onMatch>ACCEPT</onMatch> | ||
| <onMismatch>DENY</onMismatch> | ||
| </filter> | ||
| <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> | ||
| <fileNamePattern>${LOG_PATH}/info.%d{yyyy-MM-dd}.%i.log</fileNamePattern> | ||
| <maxFileSize>10MB</maxFileSize> | ||
| <maxHistory>30</maxHistory> | ||
| </rollingPolicy> | ||
| </appender> | ||
|
|
||
| <!-- WARN 로그 파일 (텍스트) --> | ||
| <appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
| <file>${LOG_PATH}/warn.log</file> | ||
| <encoder> | ||
| <pattern>${file.format}</pattern> | ||
| </encoder> | ||
| <filter class="ch.qos.logback.classic.filter.LevelFilter"> | ||
| <level>WARN</level> | ||
| <onMatch>ACCEPT</onMatch> | ||
| <onMismatch>DENY</onMismatch> | ||
| </filter> | ||
| <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> | ||
| <fileNamePattern>${LOG_PATH}/warn.%d{yyyy-MM-dd}.%i.log</fileNamePattern> | ||
| <maxFileSize>10MB</maxFileSize> | ||
| <maxHistory>30</maxHistory> | ||
| </rollingPolicy> | ||
| </appender> | ||
|
|
||
| <!-- ERROR 로그 파일 (텍스트) --> | ||
| <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
| <file>${LOG_PATH}/error.log</file> | ||
| <encoder> | ||
| <pattern>${file.format}</pattern> | ||
| </encoder> | ||
| <filter class="ch.qos.logback.classic.filter.LevelFilter"> | ||
| <level>ERROR</level> | ||
| <onMatch>ACCEPT</onMatch> | ||
| <onMismatch>DENY</onMismatch> | ||
| </filter> | ||
| <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> | ||
| <fileNamePattern>${LOG_PATH}/error.%d{yyyy-MM-dd}.%i.log</fileNamePattern> | ||
| <maxFileSize>10MB</maxFileSize> | ||
| <maxHistory>30</maxHistory> | ||
| </rollingPolicy> | ||
| </appender> | ||
|
|
||
| <root level="INFO"> | ||
| <appender-ref ref="CONSOLE" /> | ||
| <appender-ref ref="INFO_FILE" /> | ||
| <appender-ref ref="WARN_FILE" /> | ||
| <appender-ref ref="ERROR_FILE" /> | ||
| </root> | ||
| </configuration> |
Oops, something went wrong.
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.
이부분에 MDC.clear()를 넣은 이유가 따로 있을까요? 기능적으로 문제는 없어 보이지만, MDCLoggingFilter에서 이미 finally 블록으로 MDC.clear()를 처리하고 있기 떄문에 JwtFilter의 clear() 호출은 중복되어 불필요하다는 생각이 듭니다!
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.
주연님 말씀이 맞는 것 같습니다 사실상 중복 코드인 것 같아요. 처음에는 필터단에서의 동작 방식 자체에 대해 약간 부족하게 알고있기도 했고, 혹시나하는 마음에 MDC 를 put 하는 위치마다 모두 clear 를 해주도록 했었는데 그럴 필요가 없을 것 같네용 좋은 지적 감사합니다!
다만 이 브랜치를 베이스 삼아 다음 PR을 열어두어었다보니 잘못하면 커밋 내역이 꼬여버릴 것 같아서 이 부분은 추후에 고쳐둘게요!