package roomescape.common.log import com.zaxxer.hikari.HikariDataSource import net.ttddyy.dsproxy.listener.logging.SLF4JLogLevel import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder import org.springframework.beans.factory.annotation.Qualifier import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.boot.jdbc.DataSourceBuilder import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Primary import org.springframework.context.annotation.Profile import javax.sql.DataSource @Configuration @Profile("deploy") @EnableConfigurationProperties(SlowQueryProperties::class) class ProxyDataSourceConfig { @Bean @Primary fun dataSource( @Qualifier("actualDataSource") actualDataSource: DataSource, properties: SlowQueryProperties ): DataSource = ProxyDataSourceBuilder.create(actualDataSource) .name(properties.loggerName) .listener( MDCAwareSlowQueryListenerWithoutParams( logLevel = SLF4JLogLevel.nullSafeValueOf(properties.logLevel.uppercase()), thresholdMs = properties.thresholdMs ) ) .buildProxy() @Bean @ConfigurationProperties(prefix = "spring.datasource.hikari") fun actualDataSource(): DataSource = DataSourceBuilder.create() .type(HikariDataSource::class.java) .build() } @Profile("deploy") @ConfigurationProperties(prefix = "slow-query") data class SlowQueryProperties( val loggerName: String, val logLevel: String, val thresholdMs: Long, )