Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Crashlytics log caught exception

Looking for some clarification as to how one can log caught exceptions using flutter's firebase_crashlytics package.

If I understand correctly (and from running some sample code) Crashlytics.instance.log('text'); will only add logs to the next crash report, rather than send off a non-fatal issue itself.

I'm looking for functionality which is equivalent to Crashlytics.logException(e); on Android, e.g.

try {
  throwException();
} catch (e) {
  Crashlytics.logException(e);
}

which allows you to log caught exceptions so they appear as non-fatal issues in the Crashlytics dashboard.

Is this currently possible with flutter's firebase_crashlytics package?

Is calling Crashlytics.instance.recordError('text', StackTrace.current) the way to achieve this for now?

Many thanks!

like image 494
Alfred Jingle Avatar asked Sep 18 '19 16:09

Alfred Jingle


2 Answers

To add to the accepted answer, Crashlytics.instance.recordError() has now been deprecated for the new method FirebaseCrashlytics.instance.recordError(exception, stack).

BONUS TIP: I had this problem where all the logged exceptions are grouped under the same issue in Crashlytics dashboard. These might be different crashes of the same or different code components. Due to this, I had to manually go through each instance of the crash to verify.

enter image description here

From my own testing, I found out the grouping is based on the top-most line in the stack trace you passed into the method above. Luckily, Dart has an easy way to get the current stack trace using StackTrace.current.

So to properly group the issues: get the current stack trace at the time of the exception and pass it in FirebaseCrashlytics.instance.recordError(exception, stack).

Hope this helps someone out there, I looked everywhere on the internet for a similar issue but can't find any.

like image 194
Hafiz Nordin Avatar answered Nov 05 '22 06:11

Hafiz Nordin


Short answer, yes.

Crashlytics.instance.recordError() is the equivalent of Crashlytics.logException()

If you dig into the Flutter firebase_crashlytics source code, you can actually see what Android APIs are involved.

Flutter’s recordError() invokes the method Crashlytics#onError in the Android library.

And tracing Crashlytics#onError, you’ll see that it goes to Crashlytics.logException(exception);

Additional note, you’ll also notice why Crashlytics.instance.log() ”will only add logs to the next crash report”. These logs are added to a ListQueue<String> _logs which is then packaged into the next call of recordError()

A snippet of Flutter’s invocation of Crashlytics.logException():

_recordError(...) {
     ...
     final String result = await channel
          .invokeMethod<String>('Crashlytics#onError', <String, dynamic>{
        'exception': "${exception.toString()}",
        'context': '$context',
        'information': _information,
        'stackTraceElements': stackTraceElements,
        'logs': _logs.toList(),
        'keys': _prepareKeys(),
      });
}

And some reference notes for Crashlytics.logException():

To reduce your users’ network traffic, Crashlytics batches logged exceptions together and sends them the next time the app launches.

For any individual app session, only the most recent 8 logged exceptions are stored.

like image 41
TWL Avatar answered Nov 05 '22 06:11

TWL