From 2af0923189e5e736f7d0f8ce816070692b2cc233 Mon Sep 17 00:00:00 2001 From: pricelees Date: Sat, 2 Aug 2025 15:49:52 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20MDCAwareSlowQueryListenerWithoutPar?= =?UTF-8?q?ams=20=EC=97=90=EC=84=9C=20=EB=B3=80=ED=99=98,=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=EB=A1=9C=EC=A7=81=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MDCAwareSlowQueryListenerWithoutParams.kt | 28 +++++++++++++------ ...AwareSlowQueryListenerWithoutParamsTest.kt | 26 +++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 src/test/kotlin/roomescape/common/log/MDCAwareSlowQueryListenerWithoutParamsTest.kt diff --git a/src/main/kotlin/roomescape/common/log/MDCAwareSlowQueryListenerWithoutParams.kt b/src/main/kotlin/roomescape/common/log/MDCAwareSlowQueryListenerWithoutParams.kt index a1084f1e..bbc44024 100644 --- a/src/main/kotlin/roomescape/common/log/MDCAwareSlowQueryListenerWithoutParams.kt +++ b/src/main/kotlin/roomescape/common/log/MDCAwareSlowQueryListenerWithoutParams.kt @@ -4,26 +4,38 @@ import net.ttddyy.dsproxy.ExecutionInfo import net.ttddyy.dsproxy.QueryInfo import net.ttddyy.dsproxy.listener.logging.SLF4JLogLevel import net.ttddyy.dsproxy.listener.logging.SLF4JQueryLoggingListener +import java.util.function.Predicate -class MDCAwareSlowQueryListenerWithoutParams( - private val thresholdMs: Long -) : SLF4JQueryLoggingListener() { +class MDCAwareSlowQueryListenerWithoutParams : SLF4JQueryLoggingListener { + private val slowQueryPredicate: SlowQueryPredicate + private val sqlLogFormatter: SqlLogFormatter - init { - this.logLevel = SLF4JLogLevel.WARN + constructor(logLevel: SLF4JLogLevel, thresholdMs: Long) { + this.logLevel = logLevel + this.slowQueryPredicate = SlowQueryPredicate(thresholdMs) + this.sqlLogFormatter = SqlLogFormatter() } override fun afterQuery( execInfo: ExecutionInfo, queryInfoList: List ) { - if (execInfo.elapsedTime >= thresholdMs) { + if (slowQueryPredicate.test(execInfo.elapsedTime)) { super.afterQuery(execInfo, queryInfoList) } } override fun writeLog(message: String) { - val modified = message.replace(Regex("""(,?\s*)Params:\[.*?]"""), "") - super.writeLog(modified) + super.writeLog(sqlLogFormatter.maskParams(message)) } } + +class SqlLogFormatter { + fun maskParams(message: String) = message.replace(Regex("""(,?\s*)Params:\[.*?]"""), "") +} + +class SlowQueryPredicate( + private val thresholdMs: Long +) : Predicate { + override fun test(t: Long): Boolean = (t >= thresholdMs) +} diff --git a/src/test/kotlin/roomescape/common/log/MDCAwareSlowQueryListenerWithoutParamsTest.kt b/src/test/kotlin/roomescape/common/log/MDCAwareSlowQueryListenerWithoutParamsTest.kt new file mode 100644 index 00000000..ac224f63 --- /dev/null +++ b/src/test/kotlin/roomescape/common/log/MDCAwareSlowQueryListenerWithoutParamsTest.kt @@ -0,0 +1,26 @@ +package roomescape.common.log + +import io.kotest.assertions.assertSoftly +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class MDCAwareSlowQueryListenerWithoutParamsTest : StringSpec({ + "SQL 메시지에서 Params 항목은 가린다." { + val message = """Query:["select * from members m where m.email=?"], Params:[(a@a.a)]""" + val expected = """Query:["select * from members m where m.email=?"]""" + val result = SqlLogFormatter().maskParams(message) + + result shouldBe expected + } + + "입력된 thresholdMs 보다 소요시간이 긴 쿼리를 기록한다." { + val slowQueryThreshold = 10L + val slowQueryPredicate = SlowQueryPredicate(thresholdMs = slowQueryThreshold) + + assertSoftly(slowQueryPredicate) { + it.test(slowQueryThreshold) shouldBe true + it.test(slowQueryThreshold + 1) shouldBe true + it.test(slowQueryThreshold - 1) shouldBe false + } + } +})