flutter-fimber icon indicating copy to clipboard operation
flutter-fimber copied to clipboard

Add Crashlytics `LogTree` code snippet or separate plugin

Open magillus opened this issue 6 years ago • 6 comments

magillus avatar Nov 27 '18 22:11 magillus

Hi Magillus,

I got part of the code snippet this so far in main:

import 'package:flutter/foundation.dart';


void main() {
  init();
  runApp(MyApp());
}

dynamic init(){

  // sets logging up to only be on in debug mode
  if(kReleaseMode){ // is Release Mode ??
    // typically if using a crash analytics lib than set a analtycistree here via Fimber lib
    print('release mode');
  } else {
    Fimber.plantTree(DebugTree());
  }



}

I just do not have time to do the rest yet, maybe August for rest of it.

but the rest is just creating a different debug tree for the analytics lib package to use ..so it should be simple to extend this to the full code snippet for the fimber code example.

Note: I have set implicit-dynamic to true so it might be different if one is using implicit-dynamic false in the analysis_options.yaml file

Let me know if you do not understand.

Oh before I forget, you set the logger in a globals library dart file, obviously

Happy Flutter Dart'ing

fredgrott avatar Jul 03 '19 11:07 fredgrott

This is good start, It would be new CrashlyticsTree that I assume would be in fimber_crashlytics package or and sub directory of the main repository - similar to flutter_fimber. That plugin package would have iOS and Android mappings for passing logs and crashes to Crashlytics. I think it could extend flutter_fimber

I like that use of kReleaseMode and exactly similar is used with Timber on Android. On top of this extra CrashlyticsTree we need to update log levels supported for that submission to only include error and warning maybe at some special flag/build or feature flag add info log levels.

magillus avatar Jul 03 '19 13:07 magillus

Also would be nice to have hooks for say Sentry.io which the Flutter team uses and maybe bugsnag support at some point as there are some android and ios devs that use bugsnap. So maybe startout with a generic custom DebugTree and show how to slightly configure it for the crash analytics plugin and than later show configs for Sentry.io and bugsnag

fredgrott avatar Jul 04 '19 13:07 fredgrott

I think what you are proposing is something I'm looking for. What I'd like to have is Fimber handle all logging for a development build, and defer to Crashlytics for a release/production one.

I've not worked with Flutter/Dart long and I'm coming from this with a Kotlin/Java background, so this might be very naive, but couldn't one simply extend the Fimber class, call it FimberC or whatever they wish, and have that either use Fimber as usual, or simply call to Crashlytics as is normal for it based on a switch they would set? Something like, Crashlytics for prod only Y/N.

It seems like this would be trivial to do (at least in Kotlin/Java land it would) and could just be part of the documentation instead of having to come up with a new version.

darrinps avatar Jan 23 '20 20:01 darrinps

@darrinps you can override LogTree or DebugTree and add to the tree, based on debug flag you can add or not to add such tree - this would give you option to add on loggers if needed and CrashlyticsTree would be optional. See also that DebugTree take level's list to filter by.

I will welcome PR if that is something you create and want to share.

Reason why Timber/Fimber is so popular that calling logging code doesn't change - you provide different (plantTree) implementation of the code that uses the logs.

magillus avatar Jan 24 '20 00:01 magillus

If anybody is still looking for this, I'm currently using the following implementation with Crashlytics for Flutter:

CrashReportingTree:

import 'package:fimber/fimber_base.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';

class CrashReportingTree extends LogTree {
  //Only Log Warnings and Exceptions
  static const List<String> defaultLevels = ["W", "E"];
  final List<String> logLevels;

  @override
  List<String> getLevels() => logLevels;

  CrashReportingTree({this.logLevels = defaultLevels});

  @override
  void log(String level, String message, {String tag, dynamic ex, StackTrace stacktrace}) {
    final crashlytics = Crashlytics.instance;
    crashlytics.log(message);

    if (ex != null) {
      crashlytics.recordError(ex, stacktrace);
    }
  }
}

Usage:

if(kReleaseMode) {
    Fimber.plantTree(CrashReportingTree());
} else {
    Fimber.plantTree(DebugTree());
}

Also, if you see any issues with that snippet I'd love to know. The only issue I see is that Crashlytics for Flutter currently doesn't provide the possibility to add a level to the Crashlytics.log call.

lbloder avatar Jun 02 '20 20:06 lbloder