generated from pricelees/issue-pr-template
refactor: MDCAwareSlowQueryListenerWithoutParams 에서 변환, 검증로직 클래스 분리 및 테스트 추가
This commit is contained in:
parent
d293b56b0f
commit
2af0923189
@ -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<QueryInfo>
|
||||
) {
|
||||
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<Long> {
|
||||
override fun test(t: Long): Boolean = (t >= thresholdMs)
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user