Skip to content

Fix SLF4J conflict with Kotlin scripting, default log level #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies {
implementation("com.squareup.retrofit2:converter-scalars:2.11.0")
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
implementation("org.slf4j:slf4j-api:2.0.11")
implementation("ch.qos.logback:logback-classic:1.4.14")
runtimeOnly("org.slf4j:slf4j-simple:2.0.11")
testImplementation("com.squareup.okhttp3:mockwebserver:4.12.0")
testImplementation("com.squareup.okio:okio:3.9.0")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.0")
Expand Down
21 changes: 15 additions & 6 deletions library/src/main/kotlin/com/gabrielfeo/develocity/api/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@ import kotlin.time.Duration.Companion.days
data class Config(

/**
* Forces a log level for internal library classes. By default, the library includes
* logback-classic with logging disabled, aimed at notebook and script usage. Logging may be
* enabled for troubleshooting by changing log level to "INFO", "DEBUG", etc.
* Changes the default log level for library classes, such as the HTTP client. By default,
* log level is the value of
* [org.slf4j.simpleLogger.defaultLogLevel](https://www.slf4j.org/api/org/slf4j/simple/SimpleLogger.html)
* system property or `"off"`.
*
* To use different SLF4J bindings, simply exclude the logback dependency.
* Possible values:
* - "off" (default)
* - "error"
* - "warn"
* - "info"
* - "debug" (logs HTTP traffic: URLs and status codes only)
* - "trace" (logs HTTP traffic: full request and response including body, excluding
* authorization header)
*/
val logLevel: String? =
env["DEVELOCITY_API_LOG_LEVEL"],
val logLevel: String =
env["DEVELOCITY_API_LOG_LEVEL"]
?: "off",

/**
* Provides the URL of a Develocity API instance REST API. By default, uses
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.gabrielfeo.develocity.api.internal

import ch.qos.logback.classic.Level
import com.gabrielfeo.develocity.api.Config
import org.slf4j.Logger
import kotlin.reflect.KClass
Expand All @@ -14,9 +13,16 @@ class RealLoggerFactory(
) : LoggerFactory {

override fun newLogger(cls: KClass<*>): Logger {
val impl = org.slf4j.LoggerFactory.getLogger(cls.java) as ch.qos.logback.classic.Logger
return impl.apply {
level = Level.valueOf(config.logLevel)
setLogLevel()
return org.slf4j.LoggerFactory.getLogger(cls.java)
}

private fun setLogLevel() {
if (System.getProperty(SIMPLE_LOGGER_LOG_LEVEL) != null) {
return
}
System.setProperty(SIMPLE_LOGGER_LOG_LEVEL, config.logLevel)
}
}

internal const val SIMPLE_LOGGER_LOG_LEVEL = "org.slf4j.simpleLogger.defaultLogLevel"
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,12 @@ private fun OkHttpClient.Builder.addNetworkInterceptors(
if (config.cacheConfig.cacheEnabled) {
addNetworkInterceptor(buildCacheEnforcingInterceptor(config))
}
val httpLogger = loggerFactory.newLogger(HttpLoggingInterceptor::class)
getHttpLoggingInterceptorForLogger(httpLogger)?.let {
addNetworkInterceptor(it)
}
val logger = loggerFactory.newLogger(HttpLoggingInterceptor::class)
addNetworkInterceptor(HttpLoggingInterceptor(logger = logger::debug).apply { level = BASIC })
addNetworkInterceptor(HttpLoggingInterceptor(logger = logger::trace).apply { level = BODY })
addNetworkInterceptor(HttpBearerAuth("bearer", config.apiToken()))
}

private fun getHttpLoggingInterceptorForLogger(logger: Logger): Interceptor? = when {
logger.isDebugEnabled -> HttpLoggingInterceptor(logger = logger::debug).apply { level = BASIC }
logger.isTraceEnabled -> HttpLoggingInterceptor(logger = logger::debug).apply { level = BODY }
else -> null
}

internal fun buildCache(
config: Config,
loggerFactory: LoggerFactory,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.gabrielfeo.develocity.api.internal

import kotlin.test.Test
import com.gabrielfeo.develocity.api.Config
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.assertEquals

class LoggerFactoryTest {

@BeforeTest
@AfterTest
fun cleanup() {
System.clearProperty(SIMPLE_LOGGER_LOG_LEVEL)
env = FakeEnv("DEVELOCITY_API_URL" to "https://example.com/")
}

@Test
fun `Logging off by default`() {
val loggerFactory = RealLoggerFactory(Config())
loggerFactory.newLogger(LoggerFactoryTest::class)
assertEquals("off", System.getProperty(SIMPLE_LOGGER_LOG_LEVEL))
}

@Test
fun `Pre-existing defaultLogLevel is honored`() {
val loggerFactory = RealLoggerFactory(Config(logLevel = "bar"))
System.setProperty(SIMPLE_LOGGER_LOG_LEVEL, "foo")
loggerFactory.newLogger(LoggerFactoryTest::class)
assertEquals("foo", System.getProperty(SIMPLE_LOGGER_LOG_LEVEL))
}

@Test
fun `Logging can be set from config`() {
val loggerFactory = RealLoggerFactory(Config(logLevel = "foo"))
loggerFactory.newLogger(LoggerFactoryTest::class)
assertEquals("foo", System.getProperty(SIMPLE_LOGGER_LOG_LEVEL))
}
}