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 + } + } +})