如何禁用Exposed Framework日志?基于Kotlin Gradle的桌面应用
Absolutely! While Exposed doesn’t offer a dedicated Gradle property to toggle its logs directly, you can use Gradle to manage your logging framework configuration—effectively silencing Exposed’s verbose output while preserving your custom io.github.microutils:kotlin-logging logs. Here’s a step-by-step solution:
1. Ensure You’re Using a Configurable Logging Implementation
Exposed relies on SLF4J for logging, so you’ll need a concrete implementation like Logback (the most common choice with kotlin-logging). Add these dependencies to your build.gradle.kts if you haven’t already:
dependencies { // kotlin-logging core implementation("io.github.microutils:kotlin-logging-jvm:3.0.5") // Logback as SLF4J's runtime implementation runtimeOnly("ch.qos.logback:logback-classic:1.4.8") }
2. Define Logging Levels via Gradle Properties
Add these properties to your gradle.properties file to centralize control over log levels:
# Set the log level for your app's custom logs (adjust to DEBUG/INFO as needed) appLogLevel=INFO # Disable Exposed and SQLite driver logs (use OFF to silence entirely, ERROR for only critical issues) exposedLogLevel=OFF
3. Pass Gradle Properties to the JVM
In your build.gradle.kts, configure the JavaExec task to pass these properties as system properties, so your logging framework can read them:
tasks.withType<JavaExec> { systemProperty("app.log.level", property("appLogLevel")) systemProperty("exposed.log.level", property("exposedLogLevel")) }
4. Configure Logback to Filter Logs
Create a logback.xml file in src/main/resources and use the system properties to filter logs:
<configuration> <!-- Console appender for your custom logs --> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- Route your app's logs to the console (replace com.your.app with your actual package) --> <logger name="com.your.app" level="${app.log.level}" additivity="false"> <appender-ref ref="CONSOLE" /> </logger> <!-- Silence Exposed's core logs --> <logger name="org.jetbrains.exposed" level="${exposed.log.level}" additivity="false" /> <!-- Optional: Silence SQLite driver logs if they're also cluttering your console --> <logger name="org.sqlite" level="${exposed.log.level}" additivity="false" /> <!-- Turn off root logs to prevent other framework noise --> <root level="OFF" /> </configuration>
Quick Alternative (No Logback Config)
If you want a simpler, temporary fix without a log config file, you can directly set SLF4J system properties in Gradle (works with SLF4J’s SimpleLogger implementation):
tasks.withType<JavaExec> { // Disable Exposed logs entirely systemProperty("org.slf4j.simpleLogger.log.org.jetbrains.exposed", "off") // Keep your app's logs at INFO level (replace com.your.app with your package) systemProperty("org.slf4j.simpleLogger.log.com.your.app", "info") // Disable root logger systemProperty("org.slf4j.simpleLogger.defaultLogLevel", "off") }
This approach ensures only your kotlin-logging statements appear in the console, while Exposed’s internal logs are suppressed entirely.
内容的提问来源于stack exchange,提问作者kerosinkin

