Skip to content

Commit ec67e86

Browse files
authored
Merge pull request #5 from DevelopersBreach/shreyas/kermit-logger
Add Kermit logging library and replace println statements
2 parents f9b91d0 + f84096f commit ec67e86

File tree

9 files changed

+50
-61
lines changed

9 files changed

+50
-61
lines changed

composeApp/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ kotlin {
9898
api(libs.koin.core)
9999
implementation(libs.koin.compose)
100100
implementation(libs.koin.compose.viewmodel)
101+
implementation(libs.kermit)
101102
}
102103
desktopMain.dependencies {
103104
implementation(compose.desktop.currentOs)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.developersbreach.kotlindictionarymultiplatform
2+
3+
import co.touchlab.kermit.Logger
4+
5+
object Log {
6+
fun d(
7+
tag: String,
8+
message: String,
9+
) {
10+
Logger.d(message, tag = tag)
11+
}
12+
13+
fun i(
14+
tag: String,
15+
message: String,
16+
) {
17+
Logger.i(message, tag = tag)
18+
}
19+
20+
fun w(
21+
tag: String,
22+
message: String,
23+
) {
24+
Logger.w(message, tag = tag)
25+
}
26+
27+
fun e(
28+
tag: String,
29+
message: String,
30+
throwable: Throwable? = null,
31+
) {
32+
Logger.e(message, throwable, tag = tag)
33+
}
34+
}

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/core/KtorHttpClient.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.developersbreach.kotlindictionarymultiplatform.core
22

3+
import com.developersbreach.kotlindictionarymultiplatform.Log
34
import com.developersbreach.kotlindictionarymultiplatform.data.detail.ChatCompletionRequest
45
import com.developersbreach.kotlindictionarymultiplatform.data.detail.ChatCompletionResponse
56
import com.developersbreach.kotlindictionarymultiplatform.data.detail.ChatMessage
@@ -115,11 +116,11 @@ object KtorHttpClient {
115116
}
116117

117118
val text = response.bodyAsText()
118-
println("RAW RESPONSE:\n$text")
119+
Log.i("RawResponse", "RAW RESPONSE:\n$text")
119120

120121
// Parse response
121122
val chatResp = json.decodeFromString(ChatCompletionResponse.serializer(), text)
122-
println(chatResp)
123+
Log.i("ChatResponse", "$chatResp")
123124
val funcCall = chatResp.choices?.first()?.message?.functionCall ?: error("No function call in response")
124125

125126
// The arguments field is a JSON string: parse and decode into our DTO

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/screens/TopicListScreen.kt

Lines changed: 0 additions & 49 deletions
This file was deleted.

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/screens/detail/DetailScreen.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ import androidx.compose.foundation.lazy.items
2020
@Composable
2121
fun DetailScreen(viewModel: DetailViewModel) {
2222
val topicState by viewModel.state.collectAsState()
23-
println("check topicState: $topicState")
2423

2524
topicState?.let { topic ->
26-
println("Topic data fetched: $topic")
2725
LazyColumn(modifier = Modifier.padding(16.dp)) {
2826
item {
2927
Text(text = topic.topicName, style = MaterialTheme.typography.h5)

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/screens/detail/DetailViewModel.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.lifecycle.SavedStateHandle
44
import androidx.lifecycle.ViewModel
55
import androidx.lifecycle.viewModelScope
66
import androidx.navigation.toRoute
7+
import com.developersbreach.kotlindictionarymultiplatform.Log
78
import com.developersbreach.kotlindictionarymultiplatform.data.detail.KotlinTopicDetails
89
import com.developersbreach.kotlindictionarymultiplatform.core.KtorHttpClient
910
import kotlinx.coroutines.flow.MutableStateFlow
@@ -29,11 +30,11 @@ class DetailViewModel(
2930
viewModelScope.launch {
3031
try {
3132
val topic = KtorHttpClient.generateTopicDetails(topicId, API_KEY)
32-
println("Fetched details: $topic")
33+
Log.i("DetailViewModel", "Fetched details: $topic")
3334
_state.value = topic
34-
println("State updated with topic: $topic")
35+
Log.i("DetailViewModel", "State updated with topic: $topic")
3536
} catch (e: Exception) {
36-
println("Error fetching topic: ${e.message}")
37+
Log.e("DetailViewModel", "Error fetching topic: ${e.message}", e)
3738
}
3839
}
3940
}

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/screens/topic/TopicScreen.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import androidx.compose.runtime.collectAsState
1414
import androidx.compose.runtime.getValue
1515
import androidx.compose.ui.Modifier
1616
import androidx.compose.ui.unit.dp
17+
import com.developersbreach.kotlindictionarymultiplatform.Log
1718
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.Topic
1819

1920
@Composable
@@ -30,7 +31,7 @@ fun TopicScreen(
3031
) {
3132
items(topics) { topic ->
3233
TopicItem(topic = topic, onClick = {
33-
println("Topic clicked: ${topic.name}")
34+
Log.i("TopicScreen", "Topic clicked: ${topic.name}")
3435
onTopicClick(topic.name)
3536
})
3637
}
@@ -42,7 +43,7 @@ fun TopicItem(
4243
topic: Topic,
4344
onClick: () -> Unit,
4445
) {
45-
println("Rendering topic item: ${topic.name}")
46+
Log.i("TopicItem", "Rendering topic item: ${topic.name}")
4647

4748
Card(
4849
elevation = 4.dp,

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/screens/topic/TopicViewModel.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic
22

33
import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.viewModelScope
5+
import com.developersbreach.kotlindictionarymultiplatform.Log
56
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.Topic
67
import com.developersbreach.kotlindictionarymultiplatform.data.topic.repository.TopicRepository
78
import kotlinx.coroutines.flow.MutableStateFlow
@@ -21,11 +22,10 @@ class TopicViewModel : ViewModel() {
2122
viewModelScope.launch {
2223
try {
2324
val topics = TopicRepository.getTopics()
24-
println("Successfully fetched topics: $topics")
25+
Log.i("TopicFetch", "Successfully fetched topics: $topics")
2526
_topics.value = topics
2627
} catch (e: Exception) {
27-
println("Error fetching topics: ${e.message}")
28-
e.printStackTrace()
28+
Log.e("TopicFetch", "Error fetching topics: ${e.message}", e)
2929
}
3030
}
3131
}

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ androidx-material = "1.12.0"
1313
androidx-test-junit = "1.2.1"
1414
compose-multiplatform = "1.7.3"
1515
junit = "4.13.2"
16+
kermit = "2.0.4"
1617
kotlin = "2.1.10"
1718
kotlinx-coroutines = "1.10.1"
1819
kotlinx-serialization-json = "1.7.3"
@@ -23,6 +24,7 @@ ktlint = "12.2.0"
2324

2425
[libraries]
2526
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "navigation-compose" }
27+
kermit = { module = "co.touchlab:kermit", version.ref = "kermit" }
2628
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
2729
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
2830
junit = { group = "junit", name = "junit", version.ref = "junit" }

0 commit comments

Comments
 (0)