Jooq SQL 查询的日志记录

问题描述 投票:0回答:1

我正在尝试记录 JOOQ 的 SQL 查询。

作为参考,我使用了教程此页面

我正在使用 Kotlin 1.9、Spring Boot v2.7.13、Gradle 8.3、Jooq 3.18.2 和 R2DBC 以及 Posgtres(驱动程序 v. 1.0.2)、slf4j v 2.0.7

我的配置是:

构建.gradle:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation "org.springframework.boot:spring-boot-starter-log4j2"
    implementation "org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:${kotlinCoroutinesVersion}"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}"
    implementation "org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}"
    implementation "org.slf4j:slf4j-api:${slf4jVersion}"
    implementation "org.slf4j:slf4j-simple:${slf4jVersion}"
    implementation "org.springdoc:springdoc-openapi-webflux-ui:${springdocVersion}"
    implementation "org.springdoc:springdoc-openapi-kotlin:${springdocVersion}"
    implementation "org.springdoc:springdoc-openapi-common:${springdocVersion}"
    implementation "org.postgresql:r2dbc-postgresql:${r2dbcPostgreVersion}"
    implementation "io.r2dbc:r2dbc-pool:${r2dbcPoolVersion}"
    jooqGenerator "org.jooq:jooq-meta-extensions"
    jooqGenerator "jakarta.xml.bind:jakarta.xml.bind-api:${jakartaApi}"
}

configurations{
    all*.exclude module : 'spring-boot-starter-logging'
}

log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{ABSOLUTE} %5p [%-50c{4}] - %m%n"/>
        </Console>
    </Appenders>

    <Loggers>
        <!-- SQL execution logging is logged to the LoggerListener logger at DEBUG level -->
        <Logger name="org.jooq.tools.LoggerListener" level="debug">
            <AppenderRef ref="Console"/>
        </Logger>

        <!-- Other jOOQ related debug log output -->
        <Logger name="org.jooq" level="debug">
            <AppenderRef ref="Console"/>
        </Logger>

        <Root level="debug">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

Jooq 配置文件:

@Configuration
class JooqConfiguration(private val connectionFactory: ConnectionFactory) {

    init {
        System.setProperty("org.jooq.no-logo", "true")
        System.setProperty("org.jooq.no-tips", "true")
    }

    @Bean
    fun createContext(): DSLContext {
        val settings = Settings()
        settings.renderQuotedNames = RenderQuotedNames.EXPLICIT_DEFAULT_UNQUOTED
        settings.isExecuteLoggingSQLExceptions = true
        settings.isExecuteWithOptimisticLocking = true
        return DSL.using(connectionFactory,  SQLDialect.POSTGRES, settings.withRenderFormatted(true))
    }

}

@Configuration
class PrettyPrinter: ExecuteListener {

    override fun executeStart(ctx: ExecuteContext) {

        val create = DSL.using(
            ctx.dialect(),
            Settings().withRenderFormatted(true)
        )
        if (ctx.query() != null) {
            log.info(create.renderInlined(ctx.query()))
        } else if (ctx.routine() != null) {
            log.info(create.renderInlined(ctx.routine()))
        }
    }
}

由于某种原因,我看不到 SQL 查询执行的任何结果(它们只是不存在于日志中)——仅在出现某些 SQL 错误的情况下。 我想记录所有查询,而不是产生错误的查询。

logging jooq
1个回答
0
投票

从 jOOQ 3.18 开始,内置日志记录(以及

ExecuteListener
SPI)尚不适用于反应式查询:

您可以通过实现日志记录 R2DBC 代理或使用

org.jooq.tools.r2dbc.LoggingConnection
来解决此问题。 另请参阅这篇博文

© www.soinside.com 2019 - 2024. All rights reserved.