From 62e9267775acf45736cd33cd45a7fe2580bf2d37 Mon Sep 17 00:00:00 2001 From: Anthony Berg Date: Fri, 3 May 2024 01:13:16 +0100 Subject: [PATCH] feat(connector): add logging with KotlinLogger and reload4j --- connector/composeApp/build.gradle.kts | 7 +++++++ .../src/commonMain/resources/log4j.properties | 9 +++++++++ .../kotlin/tab/project/ProjectsScreenModel.kt | 5 ++++- .../src/desktopMain/kotlin/tab/test/TestRun.kt | 17 ++++++++++++----- connector/shared/build.gradle.kts | 2 +- 5 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 connector/composeApp/src/commonMain/resources/log4j.properties diff --git a/connector/composeApp/build.gradle.kts b/connector/composeApp/build.gradle.kts index e8cdfd6..4d234ff 100644 --- a/connector/composeApp/build.gradle.kts +++ b/connector/composeApp/build.gradle.kts @@ -11,6 +11,8 @@ val voyagerVersion = "1.0.0" val kotlinxVersion = "1.8.0" val koinVersion = "3.5.3" val kodeinVersion = "7.21.2" +val kotlinLogging = "5.1.0" +val sl4jVersion = "2.0.13" // Testing Versions val jupyterVersion = "5.10.1" @@ -62,6 +64,11 @@ kotlin { implementation("cafe.adriel.voyager:voyager-koin:$voyagerVersion") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:$kotlinxVersion") + + // Kotlin Logging + implementation("io.github.oshai:kotlin-logging-jvm:$kotlinLogging") + implementation("org.slf4j:slf4j-api:$sl4jVersion") + implementation("org.slf4j:slf4j-reload4j:$sl4jVersion") } // Testing diff --git a/connector/composeApp/src/commonMain/resources/log4j.properties b/connector/composeApp/src/commonMain/resources/log4j.properties new file mode 100644 index 0000000..d2dd40c --- /dev/null +++ b/connector/composeApp/src/commonMain/resources/log4j.properties @@ -0,0 +1,9 @@ +log4j.rootLogger=DEBUG, A1 +log4j.appender.A1=org.apache.log4j.ConsoleAppender +log4j.appender.A1.layout=org.apache.log4j.PatternLayout + +# Print the date in ISO 8601 format +log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n + +# Print only messages of level WARN or above in the package com.foo. +log4j.logger.com.foo=WARN diff --git a/connector/composeApp/src/desktopMain/kotlin/tab/project/ProjectsScreenModel.kt b/connector/composeApp/src/desktopMain/kotlin/tab/project/ProjectsScreenModel.kt index 859e689..6ce5653 100644 --- a/connector/composeApp/src/desktopMain/kotlin/tab/project/ProjectsScreenModel.kt +++ b/connector/composeApp/src/desktopMain/kotlin/tab/project/ProjectsScreenModel.kt @@ -4,12 +4,15 @@ import cafe.adriel.voyager.core.model.StateScreenModel import cafe.adriel.voyager.core.model.screenModelScope import io.anthonyberg.connector.shared.ProjectTransaction import io.anthonyberg.connector.shared.entity.Project +import io.github.oshai.kotlinlogging.KotlinLogging import kotlinx.coroutines.launch class ProjectsScreenModel ( private val db: ProjectTransaction ) : StateScreenModel(ProjectState.Loading) { + private val logger = KotlinLogging.logger {} + fun projectExists() { screenModelScope.launch { mutableState.value = ProjectState.Loading @@ -27,7 +30,7 @@ class ProjectsScreenModel ( private fun getProjects(): List { val projects = db.getProjects() - println(projects) + logger.debug { "Loaded projects from database: $projects" } return projects } diff --git a/connector/composeApp/src/desktopMain/kotlin/tab/test/TestRun.kt b/connector/composeApp/src/desktopMain/kotlin/tab/test/TestRun.kt index b33b57d..c92de8a 100644 --- a/connector/composeApp/src/desktopMain/kotlin/tab/test/TestRun.kt +++ b/connector/composeApp/src/desktopMain/kotlin/tab/test/TestRun.kt @@ -18,11 +18,13 @@ import androidx.compose.ui.unit.dp import cafe.adriel.voyager.core.screen.Screen import cafe.adriel.voyager.koin.getScreenModel import io.anthonyberg.connector.shared.entity.Action +import io.github.oshai.kotlinlogging.KotlinLogging class TestRun ( private val actions: List ) : Screen { + private val logger = KotlinLogging.logger {} private var tested = mutableStateListOf() @Composable @@ -40,30 +42,35 @@ class TestRun ( } when (val s = state) { - is TestState.Init -> println("Loading Simulator Tests") + is TestState.Init -> logger.info { "Loading Simulator Tests" } is TestState.NoSimulator -> { - running = false + logger.warn { "Could not connect to the simulator" } Text("Could not connect to the simulator!") return } is TestState.Ready -> { - println("Loaded Simulator Tests") + logger.info { "Loaded Simulator Tests" } screenModel.runAction(actions[step]) } - is TestState.Running -> println("Running Action: ${s.step}") + is TestState.Running -> logger.info { "Running Action: ${s.step}" } is TestState.Complete -> { + logger.info { "Completed test for action: ${s.step}" } tested.add(s.pass) step += 1 if (step == actions.size) { + logger.info { "Completed all tests" } screenModel.end() } else { screenModel.runAction(actions[step]) } } is TestState.Idle -> running = false - is TestState.Error -> return Text("An error occurred!") + is TestState.Error -> { + logger.error { "An error occurred!" } + return Text("An error occurred!") + } } Column( diff --git a/connector/shared/build.gradle.kts b/connector/shared/build.gradle.kts index a31ba68..83b03cf 100644 --- a/connector/shared/build.gradle.kts +++ b/connector/shared/build.gradle.kts @@ -9,7 +9,7 @@ val coroutinesVersion = "1.8.0" val ktorVersion = "2.3.9" val sqlDelightVersion = "2.0.2" val dateTimeVersion = "0.5.0" -val sl4jVersion = "2.0.12" +val sl4jVersion = "2.0.13" val vdmjVersion = "4.5.0" // Testing Dependencies