-
Notifications
You must be signed in to change notification settings - Fork 1
resolve issue #2 (초기 로깅 시스템) #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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/kr/co/lokit/api/config/logging/LogMaskingUtils.kt
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,29 @@ | ||
| package kr.co.lokit.api.config.logging | ||
|
|
||
| object LogMaskingUtils { | ||
| private val SENSITIVE_PATTERNS = listOf( | ||
| // password variants | ||
| """"password"\s*:\s*"[^"]*"""" to """"password":"***"""", | ||
| """"pwd"\s*:\s*"[^"]*"""" to """"pwd":"***"""", | ||
| // email - show first 2 chars | ||
| """"email"\s*:\s*"([^"]{2})[^"]*@([^"]+)"""" to """"email":"$1***@$2"""", | ||
| // phone number - show last 4 digits | ||
| """"phone"\s*:\s*"[^"]*([0-9]{4})"""" to """"phone":"***$1"""", | ||
| """"phoneNumber"\s*:\s*"[^"]*([0-9]{4})"""" to """"phoneNumber":"***$1"""", | ||
| // credit card - show last 4 digits | ||
| """"cardNumber"\s*:\s*"[^"]*([0-9]{4})"""" to """"cardNumber":"****-****-****-$1"""", | ||
| // token/secret | ||
| """"token"\s*:\s*"[^"]*"""" to """"token":"***"""", | ||
| """"secret"\s*:\s*"[^"]*"""" to """"secret":"***"""", | ||
| """"accessToken"\s*:\s*"[^"]*"""" to """"accessToken":"***"""", | ||
| """"refreshToken"\s*:\s*"[^"]*"""" to """"refreshToken":"***"""", | ||
| ) | ||
|
|
||
| fun mask(content: String): String { | ||
| var masked = content | ||
| SENSITIVE_PATTERNS.forEach { (pattern, replacement) -> | ||
| masked = masked.replace(Regex(pattern, RegexOption.IGNORE_CASE), replacement) | ||
| } | ||
| return masked | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/kr/co/lokit/api/config/logging/LoggingConfig.kt
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,19 @@ | ||
| package kr.co.lokit.api.config.logging | ||
|
|
||
| import org.springframework.boot.web.servlet.FilterRegistrationBean | ||
| import org.springframework.context.annotation.Bean | ||
| import org.springframework.context.annotation.Configuration | ||
| import org.springframework.core.Ordered | ||
|
|
||
| @Configuration | ||
| class LoggingConfig { | ||
| @Bean | ||
| fun mdcLoggingFilter(): MdcLoggingFilter = MdcLoggingFilter() | ||
|
|
||
| @Bean | ||
| fun mdcLoggingFilterRegistration(filter: MdcLoggingFilter): FilterRegistrationBean<MdcLoggingFilter> = | ||
| FilterRegistrationBean(filter).apply { | ||
| order = Ordered.HIGHEST_PRECEDENCE | ||
| addUrlPatterns("/*") | ||
ohksj77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
171 changes: 171 additions & 0 deletions
171
src/main/kotlin/kr/co/lokit/api/config/logging/MdcLoggingFilter.kt
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,171 @@ | ||
| package kr.co.lokit.api.config.logging | ||
|
|
||
| import jakarta.servlet.FilterChain | ||
| import jakarta.servlet.http.HttpServletRequest | ||
| import jakarta.servlet.http.HttpServletResponse | ||
| import org.slf4j.LoggerFactory | ||
| import org.slf4j.MDC | ||
| import org.springframework.http.MediaType | ||
| import org.springframework.web.filter.OncePerRequestFilter | ||
| import org.springframework.web.util.ContentCachingRequestWrapper | ||
| import org.springframework.web.util.ContentCachingResponseWrapper | ||
| import java.util.UUID | ||
|
|
||
| class MdcLoggingFilter : OncePerRequestFilter() { | ||
| private val log = LoggerFactory.getLogger(javaClass) | ||
ohksj77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| companion object { | ||
| const val REQUEST_ID = "requestId" | ||
| const val REQUEST_URI = "requestUri" | ||
| const val REQUEST_METHOD = "requestMethod" | ||
| const val CLIENT_IP = "clientIp" | ||
| const val STATUS = "status" | ||
| const val LATENCY = "latencyMs" | ||
| const val REQUEST_BODY = "requestBody" | ||
| const val RESPONSE_BODY = "responseBody" | ||
|
|
||
| private const val MAX_BODY_LENGTH = 1000 | ||
| private val LOGGABLE_CONTENT_TYPES = | ||
| setOf( | ||
| MediaType.APPLICATION_JSON_VALUE, | ||
| MediaType.APPLICATION_XML_VALUE, | ||
| MediaType.TEXT_PLAIN_VALUE, | ||
| MediaType.APPLICATION_FORM_URLENCODED_VALUE, | ||
| ) | ||
| } | ||
|
|
||
| override fun doFilterInternal( | ||
| request: HttpServletRequest, | ||
| response: HttpServletResponse, | ||
| filterChain: FilterChain, | ||
| ) { | ||
| val wrappedRequest = ContentCachingRequestWrapper(request, MAX_BODY_LENGTH) | ||
| val wrappedResponse = ContentCachingResponseWrapper(response) | ||
|
|
||
| val start = System.currentTimeMillis() | ||
|
|
||
| try { | ||
| MDC.put(REQUEST_ID, generateRequestId()) | ||
| MDC.put(REQUEST_URI, request.requestURI) | ||
| MDC.put(REQUEST_METHOD, request.method) | ||
| MDC.put(CLIENT_IP, getClientIp(request)) | ||
|
|
||
| filterChain.doFilter(wrappedRequest, wrappedResponse) | ||
| } finally { | ||
| val latency = System.currentTimeMillis() - start | ||
| val status = wrappedResponse.status | ||
|
|
||
| MDC.put(STATUS, status.toString()) | ||
| MDC.put(LATENCY, latency.toString()) | ||
|
|
||
| val requestBody = getRequestBody(wrappedRequest) | ||
| val responseBody = getResponseBody(wrappedResponse) | ||
|
|
||
| if (requestBody.isNotEmpty()) { | ||
| MDC.put(REQUEST_BODY, requestBody) | ||
| } | ||
| if (responseBody.isNotEmpty()) { | ||
| MDC.put(RESPONSE_BODY, responseBody) | ||
| } | ||
|
|
||
| logRequest(status, latency, requestBody, responseBody) | ||
|
|
||
| wrappedResponse.copyBodyToResponse() | ||
| MDC.clear() | ||
| } | ||
| } | ||
|
|
||
| private fun logRequest( | ||
| status: Int, | ||
| latency: Long, | ||
| requestBody: String, | ||
| responseBody: String, | ||
| ) { | ||
| val sb = StringBuilder() | ||
| sb.append("status=$status, latency=${latency}ms") | ||
|
|
||
| if (requestBody.isNotEmpty()) { | ||
| sb.append(", request=$requestBody") | ||
| } | ||
|
|
||
| if (isErrorStatus(status) && responseBody.isNotEmpty()) { | ||
| sb.append(", response=$responseBody") | ||
| } | ||
|
|
||
| if (isErrorStatus(status)) { | ||
| log.warn("completed: {}", sb.toString()) | ||
| } else { | ||
| log.info("completed: {}", sb.toString()) | ||
| } | ||
| } | ||
|
|
||
| private fun isErrorStatus(status: Int): Boolean = status >= 400 | ||
|
|
||
| private fun getRequestBody(request: ContentCachingRequestWrapper): String { | ||
| if (!isLoggableContentType(request.contentType)) { | ||
| return "" | ||
| } | ||
|
|
||
| val content = request.contentAsByteArray | ||
| if (content.isEmpty()) { | ||
| return "" | ||
| } | ||
|
|
||
| val body = String(content, Charsets.UTF_8) | ||
| val truncated = | ||
| if (body.length > MAX_BODY_LENGTH) { | ||
| body.substring(0, MAX_BODY_LENGTH) + "...(truncated)" | ||
| } else { | ||
| body | ||
| } | ||
|
|
||
| return LogMaskingUtils.mask(truncated) | ||
| } | ||
|
|
||
| private fun getResponseBody(response: ContentCachingResponseWrapper): String { | ||
| if (!isLoggableContentType(response.contentType)) { | ||
| return "" | ||
| } | ||
|
|
||
| val content = response.contentAsByteArray | ||
| if (content.isEmpty()) { | ||
| return "" | ||
| } | ||
|
|
||
| val body = String(content, Charsets.UTF_8) | ||
| val truncated = | ||
| if (body.length > MAX_BODY_LENGTH) { | ||
| body.substring(0, MAX_BODY_LENGTH) + "...(truncated)" | ||
| } else { | ||
| body | ||
| } | ||
|
|
||
| return LogMaskingUtils.mask(truncated) | ||
| } | ||
|
|
||
| private fun isLoggableContentType(contentType: String?): Boolean { | ||
| if (contentType == null) return false | ||
| return LOGGABLE_CONTENT_TYPES.any { contentType.contains(it, ignoreCase = true) } | ||
| } | ||
|
|
||
| private fun generateRequestId(): String = | ||
| UUID | ||
| .randomUUID() | ||
| .toString() | ||
| .replace("-", "") | ||
| .substring(0, 8) | ||
|
|
||
| private fun getClientIp(request: HttpServletRequest): String { | ||
| val xForwardedFor = request.getHeader("X-Forwarded-For") | ||
| if (!xForwardedFor.isNullOrBlank()) { | ||
| return xForwardedFor.split(",").first().trim() | ||
| } | ||
|
|
||
| val xRealIp = request.getHeader("X-Real-IP") | ||
| if (!xRealIp.isNullOrBlank()) { | ||
| return xRealIp | ||
| } | ||
|
|
||
| return request.remoteAddr ?: "unknown" | ||
| } | ||
| } | ||
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
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.
mask 추가 제가 놓쳤었는데 추가해주셔서 감사합니다.
logback 자체에 maskPattern 태그로 추가하는 방안도 혹시 고려해보셨는지 궁금합니다. ref
지금 방식은 유지보수 측면에서 좋을지 고민입니다.
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.
좋은 생각인 거 같습니다. 확장성, 안정성 면에서 불안한 설계라고 생각이 들어 수정해서 리뷰 받겠습니다