Skip to content

Commit

Permalink
Merge pull request #26 from hotwired/improve-logging
Browse files Browse the repository at this point in the history
Improve logging and expose warnings to apps
  • Loading branch information
jayohms authored Aug 21, 2023
2 parents 78454b6 + e9b7d5b commit ee76944
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion strada/src/main/kotlin/dev/hotwire/strada/Bridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Bridge internal constructor(webView: WebView) {
}

internal fun replyWith(message: Message) {
logMessage("bridgeWillReplyWithMessage", message)
logEvent("bridgeWillReplyWithMessage", message.toString())
val internalMessage = InternalMessage.fromMessage(message)
val javascript = generateJavaScript("replyWith", internalMessage.toJson().toJsonElement())
evaluate(javascript)
Expand Down
4 changes: 2 additions & 2 deletions strada/src/main/kotlin/dev/hotwire/strada/BridgeComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ abstract class BridgeComponent<in D : BridgeDestination>(
*/
fun replyTo(event: String): Boolean {
val message = receivedMessageFor(event) ?: run {
logEvent("bridgeMessageFailedToReply", "message for event '$event' was not received")
logWarning("bridgeMessageFailedToReply", "message for event '$event' was not received")
return false
}

Expand All @@ -79,7 +79,7 @@ abstract class BridgeComponent<in D : BridgeDestination>(
*/
fun replyTo(event: String, jsonData: String): Boolean {
val message = receivedMessageFor(event) ?: run {
logEvent("bridgeMessageFailedToReply", "message for event '$event' was not received")
logWarning("bridgeMessageFailedToReply", "message for event '$event' was not received")
return false
}

Expand Down
8 changes: 4 additions & 4 deletions strada/src/main/kotlin/dev/hotwire/strada/BridgeDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BridgeDelegate<D : BridgeDestination>(
bridge?.load()
}
} else {
logEvent("bridgeNotInitializedForWebView", location)
logWarning("bridgeNotInitializedForWebView", location)
}
}

Expand All @@ -46,7 +46,7 @@ class BridgeDelegate<D : BridgeDestination>(

fun replyWith(message: Message): Boolean {
bridge?.replyWith(message) ?: run {
logEvent("bridgeMessageFailedToReply", "bridge is not available")
logWarning("bridgeMessageFailedToReply", "bridge is not available")
return false
}

Expand All @@ -59,11 +59,11 @@ class BridgeDelegate<D : BridgeDestination>(

internal fun bridgeDidReceiveMessage(message: Message): Boolean {
return if (destinationIsActive && location == message.metadata?.url) {
logMessage("bridgeDidReceiveMessage", message)
logEvent("bridgeDidReceiveMessage", message.toString())
getOrCreateComponent(message.component)?.didReceive(message)
true
} else {
logMessage("bridgeDidIgnoreMessage", message)
logWarning("bridgeDidIgnoreMessage", message.toString())
false
}
}
Expand Down
4 changes: 2 additions & 2 deletions strada/src/main/kotlin/dev/hotwire/strada/JsonExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ internal inline fun <reified T> T.toJson() = json.encodeToString(this)
internal inline fun <reified T> JsonElement.decode(): T? = try {
json.decodeFromJsonElement<T>(this)
} catch (e: Exception) {
StradaLog.e("jsonElementDecodeException: ${e.stackTraceToString()}")
logError("jsonElementDecodeException", e)
null
}

internal inline fun <reified T> String.decode(): T? = try {
json.decodeFromString<T>(this)
} catch (e: Exception) {
StradaLog.e("jsonStringDecodeException: ${e.stackTraceToString()}")
logError("jsonStringDecodeException", e)
null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ class KotlinXJsonConverter : StradaJsonConverter() {
}

fun logException(e: Exception) {
logEvent("kotlinXJsonConverterFailedWithError", e.toString())
logError("kotlinXJsonConverterFailedWithError", e)
}
}
27 changes: 18 additions & 9 deletions strada/src/main/kotlin/dev/hotwire/strada/StradaLog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ import android.util.Log
internal object StradaLog {
private const val DEFAULT_TAG = "StradaLog"

internal fun d(msg: String) = log(Log.DEBUG, DEFAULT_TAG, msg)
private val debugEnabled get() = Strada.config.debugLoggingEnabled

internal fun e(msg: String) = log(Log.ERROR, DEFAULT_TAG, msg)
internal fun d(msg: String) = log(Log.DEBUG, msg)

private fun log(logLevel: Int, tag: String, msg: String) {
internal fun w(msg: String) = log(Log.WARN, msg)

internal fun e(msg: String) = log(Log.ERROR, msg)

private fun log(logLevel: Int, msg: String) {
when (logLevel) {
Log.DEBUG -> if (Strada.config.debugLoggingEnabled) Log.d(tag, msg)
Log.ERROR -> Log.e(tag, msg)
Log.DEBUG -> if (debugEnabled) Log.d(DEFAULT_TAG, msg)
Log.WARN -> Log.w(DEFAULT_TAG, msg)
Log.ERROR -> Log.e(DEFAULT_TAG, msg)
}
}
}

internal fun logMessage(event: String, message: Message) {
logEvent(event, message.toString())
}

internal fun logEvent(event: String, details: String = "") {
StradaLog.d("$event ".padEnd(35, '.') + " [$details]")
}

internal fun logWarning(event: String, details: String) {
StradaLog.w("$event ".padEnd(35, '.') + " [$details]")
}

internal fun logError(event: String, error: Exception) {
StradaLog.e("$event: ${error.stackTraceToString()}")
}

0 comments on commit ee76944

Please sign in to comment.