Skip to content

Fix/crashlytics groups Napier error messages #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions app/src/main/kotlin/nl/q42/template/logging/CrashlyticsLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,50 @@ class CrashlyticsLogger : Antilog() {

val limitedMessage = message?.take(MAX_CHARS_IN_LOG) ?: "(no message)" // to avoid OutOfMemoryError's

// at least one of message or throwable is not null
if (priority < LogLevel.ERROR) {
// at least one of message or throwable is not null
val errorMessage = throwable?.let {
" with error: ${throwable}: ${throwable.message}".take(MAX_CHARS_IN_LOG)
} ?: ""
Firebase.crashlytics.log(limitedMessage + errorMessage)
} else {
Firebase.crashlytics.log("recordException with message: $limitedMessage")
Firebase.crashlytics.recordException(throwable ?: Exception(message))
Firebase.crashlytics.recordException(throwable ?: buildCrashlyticsSyntheticException(limitedMessage))
}
}

/** Strip the stacktrace so that the calls implementing error logging are removed and the actual
* code that called Napier.e() remains.
*
* This is a workaround for the fact that Crashlytics groups errors by stacktrace
* [https://stackoverflow.com/a/59779764](https://stackoverflow.com/a/59779764)
*/
private fun buildCrashlyticsSyntheticException(message: String): Exception {
val stackTrace = Thread.currentThread().stackTrace
val numToRemove = 9
val lastToRemove = stackTrace.getOrNull(numToRemove - 1)
if (lastToRemove == null) {
logcatLogger.log(priority = LogLevel.ERROR, tag = null, throwable = null,
message = "Got unexpected stacktrace while logging a message: ${stackTrace.contentToString()}"
)
return SyntheticException(message, stackTrace)
}
if (lastToRemove.className != io.github.aakira.napier.Napier::class.java.name || lastToRemove.methodName != "e\$default"){
logcatLogger.log(priority = LogLevel.ERROR, tag = null, throwable = null,
message = "Got unexpected stacktrace: class: ${lastToRemove.className}, method: ${lastToRemove.methodName}"
)
}
val abbreviatedStackTrace = stackTrace.takeLast(stackTrace.size - numToRemove).toTypedArray()
return SyntheticException(message, abbreviatedStackTrace)
}

}

class SyntheticException(
message: String,
private val abbreviatedStackTrace: Array<StackTraceElement>
) : Exception(message) {
override fun getStackTrace(): Array<StackTraceElement> {
return abbreviatedStackTrace
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ class HomeViewModel @Inject constructor(
}

fun onOpenSecondScreenClicked() {
Napier.e(RuntimeException("Open Second Screen tapped. This will be shown as the non-fatal title")) {
"Open Second Screen Tapped. This will be shown in the Crashlytics breadcrumbs"
}
Napier.e { "Open Second Screen tapped. This will be shown In LogCat and on prod builds also as as the title of a Non-Fatal event" }
navigateTo(HomeSecondScreenDestination(title = "Hello world!"))
}

Expand Down