mt0s
mt0s

Reputation: 5821

How to log extra data during a crash

From time to time I'm getting a NPE in an app I have published on Play Store and I'd like to log some extra info so I can better understand what's wrong.

Therefore I followed this guide from Fabric for enhanced reports hoping that I will see the value from a variable I log in the stack trace.

This is the code I used:

id = table.getId();    // <------ line that throws NPE
Crashlytics.log("userIsOnline: " + isOnline);   // <----- variable I want to know it's value

Today I got the stack trace from two crashes but no variable was logged. Isn't this how I should log it or is there something else I haven't understand ?

Upvotes: 1

Views: 466

Answers (3)

Anupam
Anupam

Reputation: 2853

Log non-fatal exceptions
In addition to automatically reporting your app’s crashes, Crashlytics lets you log non-fatal exceptions.

On Android, that means you can log caught exceptions in your app’s catch blocks:

try {
    methodThatThrows();
} catch (Exception e) {
    Crashlytics.logException(e);
    // handle your exception here
}

Upvotes: 0

Stanislav Bondar
Stanislav Bondar

Reputation: 6245

As official docs says

Crashlytics.log() will only write to the Crashlytics crash report.

Use it above id = table.getId(); and check data in Non-Fatals tab in Fabric.

Upvotes: 1

Sanjay Bhalani
Sanjay Bhalani

Reputation: 2454

You can use FirebaseAnalytics for log event during a crash. You can use it to log events with the logEvent() method.

The following example show how to log a suggested SELECT_CONTENT Event:

val bundle = Bundle()
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, id)
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name)
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image")
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, 
bundle)

You can log your own custom events as shown in this example:

val params = Bundle()
params.putString("image_name", name)
params.putString("full_text", text)
firebaseAnalytics.logEvent("share_image", params)

Hope the above example will help you.

Upvotes: 1

Related Questions