Integrating Firebase Crashlytics to the logger

To report errors in Firebase Crashlytics, you can extend the CodenicLogger class and override the error method:

class FirebaseLogger extends CodenicLogger {
  // When a UID is assigned to the logger, then set the 
  // Firebase Crashlytics's user identifier to the same UID
  @override
  set userId(String? userId) {
    super.userId = userId;
    FirebaseCrashlytics.instance.setUserIdentifier(userId ?? '');
  }

  @override
  void error(
    MessageLog messageLog, {
    dynamic error,
    StackTrace? stackTrace,
  }) {
    super.error(messageLog, error: error, stackTrace: stackTrace);

    // When an error is logged, submit the error to Firebase Crashlytics
    FirebaseCrashlytics.instance.recordError(
      error,
      stackTrace,
      reason: messageLog,
    );
  }
}

Last updated