Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ repositories {

dependencies {
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework:spring-aspects")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-restclient")
implementation("net.logstash.logback:logstash-logback-encoder:8.0")
// implementation("org.springframework.boot:spring-boot-starter-security")
// implementation("org.springframework.boot:spring-boot-starter-security-oauth2-client")
implementation("org.springframework.boot:spring-boot-starter-webmvc")
Expand Down
29 changes: 29 additions & 0 deletions src/main/kotlin/kr/co/lokit/api/config/logging/LogMaskingUtils.kt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mask 추가 제가 놓쳤었는데 추가해주셔서 감사합니다.

logback 자체에 maskPattern 태그로 추가하는 방안도 혹시 고려해보셨는지 궁금합니다. ref

지금 방식은 유지보수 측면에서 좋을지 고민입니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 생각인 거 같습니다. 확장성, 안정성 면에서 불안한 설계라고 생각이 들어 수정해서 리뷰 받겠습니다

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 src/main/kotlin/kr/co/lokit/api/config/logging/LoggingConfig.kt
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("/*")
}
}
171 changes: 171 additions & 0 deletions src/main/kotlin/kr/co/lokit/api/config/logging/MdcLoggingFilter.kt
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)

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"
}
}
17 changes: 15 additions & 2 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%X{requestId}] [%X{clientIp}] [%X{requestMethod} %X{requestUri}] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<appender name="JSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<includeMdcKeyName>requestId</includeMdcKeyName>
<includeMdcKeyName>requestUri</includeMdcKeyName>
<includeMdcKeyName>requestMethod</includeMdcKeyName>
<includeMdcKeyName>clientIp</includeMdcKeyName>
<includeMdcKeyName>status</includeMdcKeyName>
<includeMdcKeyName>latencyMs</includeMdcKeyName>
<includeMdcKeyName>requestBody</includeMdcKeyName>
<includeMdcKeyName>responseBody</includeMdcKeyName>
</encoder>
</appender>

Expand All @@ -26,7 +39,7 @@

<springProfile name="prod">
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="JSON"/>
</root>
</springProfile>
</configuration>