feat(connector): add logging with KotlinLogger and reload4j

This commit is contained in:
Anthony Berg 2024-05-03 01:13:16 +01:00
parent 9482ba3d97
commit 62e9267775
5 changed files with 33 additions and 7 deletions

View File

@ -11,6 +11,8 @@ val voyagerVersion = "1.0.0"
val kotlinxVersion = "1.8.0" val kotlinxVersion = "1.8.0"
val koinVersion = "3.5.3" val koinVersion = "3.5.3"
val kodeinVersion = "7.21.2" val kodeinVersion = "7.21.2"
val kotlinLogging = "5.1.0"
val sl4jVersion = "2.0.13"
// Testing Versions // Testing Versions
val jupyterVersion = "5.10.1" val jupyterVersion = "5.10.1"
@ -62,6 +64,11 @@ kotlin {
implementation("cafe.adriel.voyager:voyager-koin:$voyagerVersion") implementation("cafe.adriel.voyager:voyager-koin:$voyagerVersion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:$kotlinxVersion") 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 // Testing

View File

@ -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

View File

@ -4,12 +4,15 @@ import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.screenModelScope import cafe.adriel.voyager.core.model.screenModelScope
import io.anthonyberg.connector.shared.ProjectTransaction import io.anthonyberg.connector.shared.ProjectTransaction
import io.anthonyberg.connector.shared.entity.Project import io.anthonyberg.connector.shared.entity.Project
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class ProjectsScreenModel ( class ProjectsScreenModel (
private val db: ProjectTransaction private val db: ProjectTransaction
) : StateScreenModel<ProjectState>(ProjectState.Loading) { ) : StateScreenModel<ProjectState>(ProjectState.Loading) {
private val logger = KotlinLogging.logger {}
fun projectExists() { fun projectExists() {
screenModelScope.launch { screenModelScope.launch {
mutableState.value = ProjectState.Loading mutableState.value = ProjectState.Loading
@ -27,7 +30,7 @@ class ProjectsScreenModel (
private fun getProjects(): List<Project> { private fun getProjects(): List<Project> {
val projects = db.getProjects() val projects = db.getProjects()
println(projects) logger.debug { "Loaded projects from database: $projects" }
return projects return projects
} }

View File

@ -18,11 +18,13 @@ import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.core.screen.Screen import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.koin.getScreenModel import cafe.adriel.voyager.koin.getScreenModel
import io.anthonyberg.connector.shared.entity.Action import io.anthonyberg.connector.shared.entity.Action
import io.github.oshai.kotlinlogging.KotlinLogging
class TestRun ( class TestRun (
private val actions: List<Action> private val actions: List<Action>
) : Screen { ) : Screen {
private val logger = KotlinLogging.logger {}
private var tested = mutableStateListOf<Boolean>() private var tested = mutableStateListOf<Boolean>()
@Composable @Composable
@ -40,30 +42,35 @@ class TestRun (
} }
when (val s = state) { when (val s = state) {
is TestState.Init -> println("Loading Simulator Tests") is TestState.Init -> logger.info { "Loading Simulator Tests" }
is TestState.NoSimulator -> { is TestState.NoSimulator -> {
running = false logger.warn { "Could not connect to the simulator" }
Text("Could not connect to the simulator!") Text("Could not connect to the simulator!")
return return
} }
is TestState.Ready -> { is TestState.Ready -> {
println("Loaded Simulator Tests") logger.info { "Loaded Simulator Tests" }
screenModel.runAction(actions[step]) screenModel.runAction(actions[step])
} }
is TestState.Running -> println("Running Action: ${s.step}") is TestState.Running -> logger.info { "Running Action: ${s.step}" }
is TestState.Complete -> { is TestState.Complete -> {
logger.info { "Completed test for action: ${s.step}" }
tested.add(s.pass) tested.add(s.pass)
step += 1 step += 1
if (step == actions.size) { if (step == actions.size) {
logger.info { "Completed all tests" }
screenModel.end() screenModel.end()
} else { } else {
screenModel.runAction(actions[step]) screenModel.runAction(actions[step])
} }
} }
is TestState.Idle -> running = false 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( Column(

View File

@ -9,7 +9,7 @@ val coroutinesVersion = "1.8.0"
val ktorVersion = "2.3.9" val ktorVersion = "2.3.9"
val sqlDelightVersion = "2.0.2" val sqlDelightVersion = "2.0.2"
val dateTimeVersion = "0.5.0" val dateTimeVersion = "0.5.0"
val sl4jVersion = "2.0.12" val sl4jVersion = "2.0.13"
val vdmjVersion = "4.5.0" val vdmjVersion = "4.5.0"
// Testing Dependencies // Testing Dependencies