AAkira / Napier

Logging library for Kotlin Multiplatform
Apache License 2.0
831 stars 37 forks source link

Crashlytics groups unrelated logs #124

Open ninovanhooff opened 6 months ago

ninovanhooff commented 6 months ago

Google Crashlytics has a feature to log non-fatal errors using (Android): Firebase.crashlytics.recordException

It takes a throwable as an argument. But let's say you want to log a non-fatal event that doesn't have an Exception associated with it. For example: "Logged-in user does not exist", "Cannot handle objects of type X" etc.

I implemented it in our CrashlyticsLogger like so:

https://github.com/Q42/Template.Android/blob/191ba61cfe6785fcb8204dec07bf3b0ba189ac20/app/src/main/kotlin/nl/q42/template/logging/CrashlyticsLogger.kt#L46

As you can see, the message is simply wrapped in a generic Exception. This works and the error appears in Crashlytics non-fatal errors section.

However, all events that are logged in this way are grouped as occurrences of the same event, and it is thus impossible to estimate how often a specific event occurs because only the total of all events is shown. It seems the reason is that Crashlytics groups errors by the line of code where the Exception was created, which in all of these cases will the be line I linked to.

Te workaround would be to always explicitly wrap the message in an Exception at the call site:

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"
        }

It would be nice for developers to simply be able to call

Napier.e { "Open Second screen Tapped" }

On one idea I have to make this possible would be to make a change to napier where all functions have the inline keyword. So that they get injected in the callsite, where the Exception would then be created. Thus making the stacktrace unique and pointing to the actual file and line where a problem occurred.