Skip to content

Commit 6097b3a

Browse files
committed
Improve debug logging
1 parent 28a7616 commit 6097b3a

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package com.gabrielfeo.gradle.enterprise.api.internal
22

3+
import com.gabrielfeo.gradle.enterprise.api.debugLoggingEnabled
4+
import java.util.logging.Level.INFO
5+
import java.util.logging.Logger
6+
37
private const val DEFAULT_KEY_NAME = "gradle-enterprise-api-token"
48
private const val DEFAULT_VAR_NAME = "GRADLE_ENTERPRISE_API_TOKEN"
59

610
internal fun requireToken(
711
keyName: String = DEFAULT_KEY_NAME,
812
varName: String = DEFAULT_VAR_NAME,
9-
debugging: Boolean = false,
1013
): String {
11-
return tokenFromKeychain(keyName, debugging)
12-
?: tokenFromEnv(varName, debugging)
14+
return tokenFromKeychain(keyName)
15+
?: tokenFromEnv(varName)
1316
?: error("""
1417
No API token. Either
1518
- create a key in macOS keychain labeled $DEFAULT_KEY_NAME
@@ -18,15 +21,15 @@ internal fun requireToken(
1821
""".trimIndent())
1922
}
2023

21-
private fun tokenFromEnv(varName: String, debugging: Boolean): String? {
24+
private fun tokenFromEnv(varName: String): String? {
2225
return System.getenv(varName).also {
23-
if (debugging && it.isNullOrBlank()) {
24-
println("Env var $varName=$it")
26+
if (debugLoggingEnabled && it.isNullOrBlank()) {
27+
Logger.getGlobal().log(INFO, "Env var $varName=$it")
2528
}
2629
}
2730
}
2831

29-
private fun tokenFromKeychain(keyName: String, debugging: Boolean): String? {
32+
private fun tokenFromKeychain(keyName: String): String? {
3033
val login = System.getenv("LOGNAME")
3134
val process = ProcessBuilder(
3235
"security", "find-generic-password", "-w", "-a", login, "-s", keyName
@@ -36,8 +39,8 @@ private fun tokenFromKeychain(keyName: String, debugging: Boolean): String? {
3639
return process.inputStream.bufferedReader().use {
3740
it.readText().trim()
3841
}
39-
} else if (debugging) {
40-
println("Failed to get key from keychain (exit $status)")
42+
} else if (debugLoggingEnabled) {
43+
Logger.getGlobal().log(INFO, "Failed to get key from keychain (exit $status)")
4144
}
4245
return null
4346
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package com.gabrielfeo.gradle.enterprise.api.internal.caching
22

33
import com.gabrielfeo.gradle.enterprise.api.baseUrl
4+
import com.gabrielfeo.gradle.enterprise.api.debugLoggingEnabled
45
import com.gabrielfeo.gradle.enterprise.api.maxCacheSize
56
import okhttp3.Cache
67
import okhttp3.HttpUrl.Companion.toHttpUrl
78
import java.io.File
9+
import java.util.logging.Level.INFO
10+
import java.util.logging.Logger
811

912
internal val cache: Cache = run {
1013
val host = baseUrl().toHttpUrl().host
1114
val tempDir = System.getProperty("java.io.tmpdir")
12-
Cache(
13-
directory = File(tempDir, "gradle-enterprise-api-cache-$host"),
14-
maxSize = maxCacheSize,
15-
)
15+
val dir = File(tempDir, "gradle-enterprise-api-cache-$host")
16+
if (debugLoggingEnabled) {
17+
Logger.getGlobal().log(INFO, "HTTP cache dir: $dir")
18+
}
19+
Cache(dir, maxSize = maxCacheSize)
1620
}

0 commit comments

Comments
 (0)