Skip to content

Commit f372281

Browse files
committed
chore: include tail of app logs with errors to bugsnag
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 7cbd9ac commit f372281

6 files changed

Lines changed: 19 additions & 22 deletions

File tree

apps/flipcash/app/src/main/kotlin/com/flipcash/app/internal/debug/FlipcashBugsnagErrorCallback.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.flipcash.app.internal.debug
22

33
import com.bugsnag.android.OnErrorCallback
44
import com.getcode.utils.ErrorUtils
5+
import com.getcode.utils.TraceManager
56
import io.grpc.StatusException
67

78
internal val FlipcashErrorCallback = OnErrorCallback onError@{ event ->
@@ -20,5 +21,14 @@ internal val FlipcashErrorCallback = OnErrorCallback onError@{ event ->
2021

2122
if (!event.isUnhandled) return@onError true
2223

24+
// Attach recent logs
25+
val logFile = TraceManager.getLogFile(includeHeader = false)
26+
if (logFile != null && logFile.exists()) {
27+
// Bugsnag metadata has a size limit (~1MB per event).
28+
// Attach the last ~64KB of log content as a metadata tab.
29+
val tail = logFile.readText().takeLast(64_000)
30+
event.addMetadata("App Logs", "app_log", tail)
31+
}
32+
2333
true
2434
}

apps/flipcash/app/src/main/kotlin/com/flipcash/app/internal/startup/TraceInitializer.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ class TraceInitializer: Initializer<Unit> {
2222
Timber.plant(FlipcashDebugTree)
2323
} else {
2424
CoroutineScope(Dispatchers.IO).launch {
25-
val config = Configuration.load(context)
26-
config.addOnError(FlipcashErrorCallback)
25+
val config = Configuration.load(context).apply {
26+
addOnError(FlipcashErrorCallback)
27+
}
2728
Bugsnag.start(context, config)
2829

2930
ErrorUtils.addReporter(BugsnagErrorReporter())

apps/flipcash/app/src/screenshotTest/kotlin/com/flipcash/app/login/LoginContentScreenshotTest.kt

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

gradle/libs.versions.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ desugaring = "2.1.5"
8686
event-bus = "0.1.0"
8787
solana-mwa = "2.1.0"
8888

89-
bugsnag = "6.25.0"
89+
bugsnag = "6.26.0"
9090
bugsnag-agp = "8.2.0"
9191
bugsnag-gradle-plugin = "1.1.0"
92-
9392
rinku = "1.6.0"
9493
haze = "1.7.2"
9594
cloudy = "0.5.0"

libs/logging/src/main/kotlin/com/getcode/utils/FileTree.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,13 @@ class FileTree(
9393
logFlow.tryEmit(processed!!)
9494
}
9595

96-
fun getLogFile(): File? {
96+
fun getLogFile(includeHeader: Boolean = true): File? {
9797
if (!logFile.exists() || logFile.length() == 0L) return null
9898
synchronized(lock) {
9999
exportFile.delete()
100-
exportFile.writeText(buildDeviceHeader(appContext))
100+
if (includeHeader) {
101+
exportFile.writeText(buildDeviceHeader(appContext))
102+
}
101103
logFile.inputStream().use { input ->
102104
FileOutputStream(exportFile, true).use { output ->
103105
input.copyTo(output)

libs/logging/src/main/kotlin/com/getcode/utils/Logging.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ object TraceManager {
108108
}
109109

110110
/** Returns the current log file, or `null` if not yet initialized. */
111-
fun getLogFile(): File? = fileTree?.getLogFile()
111+
fun getLogFile(includeHeader: Boolean = true): File? = fileTree?.getLogFile(includeHeader)
112112

113113
/**
114114
* Hot stream of processed log lines for live in-app viewers. Emits only

0 commit comments

Comments
 (0)