Skip to content

Commit

Permalink
fix: log on error level
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklasl committed Feb 7, 2025
1 parent 7aecb34 commit aabce4f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
10 changes: 9 additions & 1 deletion Confidence/src/main/java/com/spotify/confidence/DebugLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal interface DebugLogger {
fun logFlag(action: String, details: String? = null)
fun logContext(action: String, context: Map<String, ConfidenceValue>)
fun logResolve(flag: String, context: JsonElement)
fun logError(message: String, throwable: Throwable? = null)
companion object {
const val TAG = "Confidence"
}
Expand Down Expand Up @@ -51,12 +52,19 @@ internal class DebugLoggerImpl(private val filterLevel: LoggingLevel, private va
)
}

override fun logError(message: String, throwable: Throwable?) {
error(message, throwable)
}

private fun verbose(message: String) = log(LoggingLevel.VERBOSE, message)
private fun debug(message: String) = log(LoggingLevel.DEBUG, message)
private fun warn(message: String, throwable: Throwable?) =
log(LoggingLevel.WARN, throwable?.let { "$message: ${throwable.message}" } ?: message)

private fun error(message: String, throwable: Throwable) = log(LoggingLevel.ERROR, "$message: ${throwable.message}")
private fun error(message: String, throwable: Throwable?) = log(
LoggingLevel.ERROR,
throwable?.let { "$message: ${throwable.message}" } ?: message
)

private fun log(messageLevel: LoggingLevel, message: String) {
if (messageLevel >= filterLevel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal class EventSenderUploaderImpl(

val response = httpClient.newCall(httpRequest).await()
if (!response.isSuccessful) {
debugLogger?.logMessage(message = "Failed to upload events. http code ${response.code}", isWarning = true)
debugLogger?.logError(message = "Failed to upload events. http code ${response.code}")
}
when (response.code) {
// clean up in case of success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ internal class RemoteFlagResolver(

private fun Response.toResolveFlags(): ResolveResponse {
if (!isSuccessful) {
debugLogger?.logMessage("Failed to resolve flags. Http code: $code", isWarning = true)
debugLogger?.logError("Failed to resolve flags. Http code: $code")
}
body?.let { body ->
val bodyString = body.string()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ internal open class DebugLoggerFake : DebugLogger {
// not important enough to test right now
}

override fun logError(message: String, throwable: Throwable?) {
messagesLogged.add(Msg(message, true, throwable))
}

data class Msg(val message: String, val isWarning: Boolean, val throwable: Throwable?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ class DebugLoggerImplTest {

errorLogger.logMessage("Error msg", throwable = Error("my error"))
verify { Log.e("Confidence", "Error msg: my error") }

errorLogger.logError("Error msg")
verify { Log.e("Confidence", "Error msg") }
}

@Test
Expand Down

0 comments on commit aabce4f

Please sign in to comment.