Skip to content

Commit 4c530e7

Browse files
authored
Merge pull request #67 from amazon-connect/xiajonDisableLogging
add toggle to enable/disable logging
2 parents 09244e7 + ce95010 commit 4c530e7

File tree

9 files changed

+105
-69
lines changed

9 files changed

+105
-69
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ interface ChatSDKLogger {
151151
fun logDebug(message: () -> String)
152152
fun logWarn(message: () -> String)
153153
fun logError(message: () -> String)
154+
fun setLoggingEnabled(enabled: Boolean)
155+
}
156+
```
157+
158+
#### `SDKLogger.setLoggingEnabled`
159+
This API will allow you to enable and disable logging.
160+
161+
```
162+
fun setLoggingEnabled(enabled: Boolean) {
163+
this.logger.setLoggingEnabled(enabled)
154164
}
155165
```
156166

app/src/main/java/com/amazon/connect/chat/androidchatexample/utils/CustomLogger.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import kotlinx.coroutines.SupervisorJob
77
import kotlinx.coroutines.launch
88
import kotlinx.coroutines.withContext
99
import java.io.File
10-
import java.io.FileNotFoundException
1110
import java.text.SimpleDateFormat
1211
import java.util.Date
1312
import java.util.Locale
@@ -25,8 +24,16 @@ class CustomLogger : ChatSDKLogger {
2524
private val currentTimeMillis = System.currentTimeMillis()
2625
private val loggerCreationDateAndTime = CommonUtils.formatDate(currentTimeMillis, false)
2726

27+
private var loggingEnabled: Boolean = true
28+
29+
//Custom logging logic
30+
override fun setLoggingEnabled(enabled: Boolean) {
31+
this.loggingEnabled = enabled
32+
}
33+
2834
override fun logVerbose(message: () -> String) {
2935
// Custom logging logic
36+
if (!loggingEnabled) return
3037
val logMessage = "VERBOSE: ${message()}"
3138
println(logMessage)
3239
coroutineScope.launch {
@@ -36,6 +43,7 @@ class CustomLogger : ChatSDKLogger {
3643

3744
override fun logInfo(message: () -> String) {
3845
// Custom logging logic
46+
if (!loggingEnabled) return
3947
val logMessage = "INFO: ${message()}"
4048
println(logMessage)
4149
coroutineScope.launch {
@@ -45,16 +53,17 @@ class CustomLogger : ChatSDKLogger {
4553

4654
override fun logDebug(message: () -> String) {
4755
// Custom logging logic
56+
if (!loggingEnabled) return
4857
val logMessage = "DEBUG: ${message()}"
4958
println(logMessage)
5059
coroutineScope.launch {
5160
writeToAppTempFile(logMessage)
5261
}
53-
5462
}
5563

5664
override fun logWarn(message: () -> String) {
5765
// Custom logging logic
66+
if (!loggingEnabled) return
5867
val logMessage = "WARN: ${message()}"
5968
println(logMessage)
6069
coroutineScope.launch {
@@ -64,6 +73,7 @@ class CustomLogger : ChatSDKLogger {
6473

6574
override fun logError(message: () -> String) {
6675
// Custom logging logic
76+
if (!loggingEnabled) return
6777
val logMessage = "ERROR: ${message()}"
6878
println(logMessage)
6979
coroutineScope.launch {
@@ -94,4 +104,4 @@ class CustomLogger : ChatSDKLogger {
94104
}
95105
}
96106
}
97-
}
107+
}

chat-sdk/src/main/java/com/amazon/connect/chat/sdk/model/MessageContent.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.amazon.connect.chat.sdk.model
55

66
import com.amazon.connect.chat.sdk.utils.Constants
7+
import com.amazon.connect.chat.sdk.utils.logger.SDKLogger
78
import kotlinx.serialization.Serializable
89
import kotlinx.serialization.SerializationException
910
import kotlinx.serialization.json.Json
@@ -72,7 +73,7 @@ data class QuickReplyContent(
7273
val subtitle = quickReply.data.content.subtitle ?: "" // Fallback to empty string if null
7374
QuickReplyContent(title, subtitle, options)
7475
} catch (e: Exception) {
75-
println("Error decoding QuickReplyContent: ${e.message}")
76+
SDKLogger.logger.logError{"MessageContent: Error decoding QuickReplyContent: ${e.message}"}
7677
null
7778
}
7879
}
@@ -130,14 +131,13 @@ data class ListPickerContent(
130131
val imageUrl = listPicker.data.content.imageData ?: ""
131132
ListPickerContent(title, subtitle, imageUrl, options)
132133
} catch (e: Exception) {
133-
println("Error decoding ListPickerContent: ${e.message}")
134+
SDKLogger.logger.logError{"MessageContent: Error decoding ListPickerContent: ${e.message}"}
134135
null
135136
}
136137
}
137138
}
138139
}
139140

140-
141141
// Time Picker
142142
@Serializable
143143
data class TimeSlot(
@@ -202,7 +202,7 @@ data class TimePickerContent(
202202
timeslots = contentData.timeslots
203203
)
204204
} catch (e: SerializationException) {
205-
println("Error decoding TimePickerContent: ${e.localizedMessage}")
205+
SDKLogger.logger.logError{"MessageContent: Error decoding TimePickerContent: ${e.localizedMessage}"}
206206
null
207207
}
208208
}
@@ -251,7 +251,7 @@ data class CarouselContent(
251251
elements = contentData.elements
252252
)
253253
} catch (e: SerializationException) {
254-
println("Error decoding CarouselContent: ${e.localizedMessage}")
254+
SDKLogger.logger.logError{"MessageContent: Error decoding CarouselContent: ${e.localizedMessage}"}
255255
null
256256
}
257257
}
@@ -314,9 +314,9 @@ data class PanelContent(
314314
options = contentData.elements
315315
)
316316
} catch (e: SerializationException) {
317-
println("Error decoding PanelContent: ${e.localizedMessage}")
317+
SDKLogger.logger.logError{"MessageContent: Error decoding PanelContent: ${e.localizedMessage}"}
318318
null
319319
}
320320
}
321321
}
322-
}
322+
}

0 commit comments

Comments
 (0)