Skip to content

Commit 3e1ac49

Browse files
authored
feat(server): improve logging setup (#1641)
Fixes #1194
1 parent 53acdc1 commit 3e1ac49

File tree

7 files changed

+72
-10
lines changed

7 files changed

+72
-10
lines changed

action-binding-generator/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies {
1515
implementation("com.squareup:kotlinpoet:2.0.0")
1616
implementation("it.krzeminski:snakeyaml-engine-kmp:3.0.3")
1717
implementation("com.charleskorn.kaml:kaml:0.62.0")
18+
implementation("io.github.oshai:kotlin-logging:7.0.0")
1819
implementation(projects.sharedInternal)
1920

2021
testImplementation(projects.githubWorkflowsKt)

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/metadata/MetadataReading.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.typesafegithub.workflows.actionbindinggenerator.metadata
22

33
import com.charleskorn.kaml.Yaml
4+
import io.github.oshai.kotlinlogging.KotlinLogging.logger
45
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
56
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.CommitHash
67
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.MetadataRevision
@@ -11,6 +12,8 @@ import kotlinx.serialization.decodeFromString
1112
import java.io.IOException
1213
import java.net.URI
1314

15+
private val logger = logger { }
16+
1417
/**
1518
* [Metadata syntax for GitHub Actions](https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions).
1619
*/
@@ -56,7 +59,7 @@ public fun ActionCoords.fetchMetadata(
5659
return list
5760
.firstNotNullOfOrNull { url ->
5861
try {
59-
println(" ... from $url")
62+
logger.info { " ... from $url" }
6063
fetchUri(URI(url))
6164
} catch (e: IOException) {
6265
null

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypesProviding.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.typesafegithub.workflows.actionbindinggenerator.typing
22

33
import com.charleskorn.kaml.Yaml
4+
import io.github.oshai.kotlinlogging.KotlinLogging.logger
45
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
56
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.CommitHash
67
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.MetadataRevision
@@ -17,6 +18,8 @@ import kotlinx.serialization.decodeFromString
1718
import java.io.IOException
1819
import java.net.URI
1920

21+
private val logger = logger { }
22+
2023
internal fun ActionCoords.provideTypes(
2124
metadataRevision: MetadataRevision,
2225
fetchUri: (URI) -> String = ::fetchUri,
@@ -54,7 +57,7 @@ private fun ActionCoords.fetchTypingMetadata(
5457
val typesMetadataYaml =
5558
list.firstNotNullOfOrNull { url ->
5659
try {
57-
println(" ... types from $url")
60+
logger.info { " ... types from action $url" }
5861
fetchUri(URI(url))
5962
} catch (e: IOException) {
6063
null
@@ -74,7 +77,7 @@ private fun ActionCoords.fetchTypingsForOlderVersionFromCatalog(fetchUri: (URI)
7477
val metadataUrl = this.catalogMetadata()
7578
val metadataYml =
7679
try {
77-
println(" ... metadata from $metadataUrl")
80+
logger.info { " ... metadata from $metadataUrl" }
7881
fetchUri(URI(metadataUrl))
7982
} catch (e: IOException) {
8083
return null
@@ -86,10 +89,10 @@ private fun ActionCoords.fetchTypingsForOlderVersionFromCatalog(fetchUri: (URI)
8689
.filter { it.versionToInt() < requestedVersionAsInt }
8790
.maxByOrNull { it.versionToInt() }
8891
?: run {
89-
println(" ... no fallback version found!")
92+
logger.info { " ... no fallback version found!" }
9093
return null
9194
}
92-
println(" ... using fallback version: $fallbackVersion")
95+
logger.info { " ... using fallback version: $fallbackVersion" }
9396
val adjustedCoords = this.copy(version = fallbackVersion)
9497
return fetchTypingsFromUrl(url = adjustedCoords.actionTypesFromCatalog(), fetchUri = fetchUri)
9598
}
@@ -100,7 +103,7 @@ private fun fetchTypingsFromUrl(
100103
): ActionTypes? {
101104
val typesMetadataYml =
102105
try {
103-
println(" ... types from $url")
106+
logger.info { " ... types from catalog $url" }
104107
fetchUri(URI(url))
105108
} catch (e: IOException) {
106109
null

jit-binding-server/build.gradle.kts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ dependencies {
1414
implementation("io.ktor:ktor-client-cio")
1515
implementation("io.ktor:ktor-client-content-negotiation")
1616
implementation("io.ktor:ktor-serialization-kotlinx-json")
17+
implementation("io.ktor:ktor-server-call-logging")
18+
implementation("io.ktor:ktor-server-call-id")
1719
implementation("io.opentelemetry.instrumentation:opentelemetry-ktor-2.0:2.3.0-alpha")
1820
implementation("io.opentelemetry:opentelemetry-sdk:1.43.0")
1921
implementation("io.opentelemetry:opentelemetry-exporter-otlp:1.43.0")
2022
implementation("io.opentelemetry:opentelemetry-exporter-logging:1.43.0")
2123
implementation("io.github.reactivecircus.cache4k:cache4k:0.13.0")
22-
implementation("ch.qos.logback:logback-classic:1.5.12")
24+
implementation("io.github.oshai:kotlin-logging:7.0.0")
25+
implementation(platform("org.apache.logging.log4j:log4j-bom:2.24.0"))
26+
implementation("org.apache.logging.log4j:log4j-jul")
27+
runtimeOnly("org.apache.logging.log4j:log4j-core")
28+
runtimeOnly("org.apache.logging.log4j:log4j-slf4j2-impl")
29+
runtimeOnly("org.apache.logging.log4j:log4j-jpl")
2330

2431
implementation(projects.mavenBindingBuilder)
2532
implementation(projects.sharedInternal)

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.typesafegithub.workflows.jitbindingserver
22

3+
import io.github.oshai.kotlinlogging.KotlinLogging.logger
34
import io.github.reactivecircus.cache4k.Cache
45
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
56
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.prettyPrint
@@ -10,12 +11,17 @@ import io.github.typesafegithub.workflows.mavenbinding.buildPackageArtifacts
1011
import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
1112
import io.github.typesafegithub.workflows.shared.internal.getGithubToken
1213
import io.ktor.http.ContentType
14+
import io.ktor.http.HttpHeaders.XRequestId
1315
import io.ktor.http.HttpStatusCode
1416
import io.ktor.server.application.ApplicationCall
1517
import io.ktor.server.application.call
1618
import io.ktor.server.application.install
1719
import io.ktor.server.engine.embeddedServer
1820
import io.ktor.server.netty.Netty
21+
import io.ktor.server.plugins.callid.CallId
22+
import io.ktor.server.plugins.callid.callIdMdc
23+
import io.ktor.server.plugins.callid.generate
24+
import io.ktor.server.plugins.callloging.CallLogging
1925
import io.ktor.server.response.respondBytes
2026
import io.ktor.server.response.respondText
2127
import io.ktor.server.routing.Route
@@ -26,7 +32,23 @@ import io.ktor.server.routing.routing
2632
import io.opentelemetry.instrumentation.ktor.v2_0.server.KtorServerTracing
2733
import kotlin.time.Duration.Companion.hours
2834

35+
private val logger =
36+
System
37+
/*
38+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
39+
* !! IMPORTANT: !!
40+
* !! This statement has to always be executed first before **any** other code, !!
41+
* !! or else the property "java.util.logging.manager" may have no effect anymore! !!
42+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
43+
*/
44+
.setProperty("java.util.logging.manager", org.apache.logging.log4j.jul.LogManager::class.java.name)
45+
.let { logger { } }
46+
2947
fun main() {
48+
Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
49+
logger.error(throwable) { "Uncaught exception in thread $thread" }
50+
}
51+
3052
val bindingsCache =
3153
Cache
3254
.Builder<ActionCoords, Result<Map<String, Artifact>>>()
@@ -35,6 +57,16 @@ fun main() {
3557
val openTelemetry = buildOpenTelemetryConfig(serviceName = "github-actions-bindings")
3658

3759
embeddedServer(Netty, port = 8080) {
60+
install(CallId) {
61+
generate(
62+
length = 15,
63+
dictionary = "abcdefghijklmnopqrstuvwxyz0123456789",
64+
)
65+
replyToHeader(XRequestId)
66+
}
67+
install(CallLogging) {
68+
callIdMdc("request-id")
69+
}
3870
install(KtorServerTracing) {
3971
setOpenTelemetry(openTelemetry)
4072
}
@@ -155,7 +187,7 @@ private suspend fun ApplicationCall.toBindingArtifacts(
155187
version = version,
156188
path = nameAndPath.drop(1).joinToString("/").takeUnless { it.isBlank() },
157189
)
158-
println("➡️ Requesting ${actionCoords.prettyPrint}")
190+
logger.info { "➡️ Requesting ${actionCoords.prettyPrint}" }
159191
val bindingArtifacts =
160192
if (refresh) {
161193
actionCoords.buildVersionArtifacts().also {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<Configuration name="github-workflows-kt-jit-binding-server" status="INFO" monitorInterval="5" strict="true">
4+
<Appenders>
5+
<Console name="Console Appender">
6+
<PatternLayout>
7+
<Pattern><![CDATA[%highlight{%d <%-5p> <%-35.35t> <%x> <%X> <%50.50c> %m}%n]]></Pattern>
8+
</PatternLayout>
9+
</Console>
10+
</Appenders>
11+
<Loggers>
12+
<Root level="INFO">
13+
<AppenderRef ref="Console Appender"/>
14+
</Root>
15+
</Loggers>
16+
</Configuration>

maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/JarBuilding.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ private fun compileBinding(sourceFilePaths: List<Path>): Path {
8181
services = Services.EMPTY,
8282
arguments = args,
8383
)
84-
require(exitCode == ExitCode.OK) {
85-
"Binding compilation failed! Compiler messages: $compilerMessagesOutputStream"
84+
check(exitCode == ExitCode.OK) {
85+
"Binding compilation failed! Compiler messages:\n$compilerMessagesOutputStream"
8686
}
8787
return compilationOutput
8888
}

0 commit comments

Comments
 (0)