SourceHorizon / logger

Small, easy to use and extensible logger which prints beautiful logs.
https://pub.dev/packages/logger
MIT License
197 stars 33 forks source link

Write errors higher than a specific level to crashlytics #21

Closed sarthakydv closed 1 year ago

sarthakydv commented 1 year ago

In our apps, we are catching lots of exceptions but in some cases, it would be good to have them reported back to us. I am currently integrating crashlytics into it.

I made a CrashlyticsLogPrinter that along with a few extra changes can work but I would still be returning an empty list. Please suggest alternatives to this approch. I am thinking that a custom LogOutput might help but it works on a line by line basis, I am not sure how I can use it to write a proper log on crashlytics.

Any advice will be deeply appreciated

import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:logger/logger.dart';

class CrashlyticsLogPrinter extends LogPrinter {
  final String _className;
  final FirebaseCrashlytics? _firebaseCrashlytics;

  KodomamoCrashlyticsLogPrinter(this._className, this._firebaseCrashlytics);

  @override
  List<String> log(LogEvent event) {
    /// This sends logs of warning level and above to Crashlytics
    if (event.level.index >= Level.warning.index) {
      _firebaseCrashlytics!.recordError(
        event.message,
        event.stackTrace,
        fatal: true,
        information: [_className, event.level],
        printDetails: false,
      );
    }
    return [];
  }
}
Bungeefan commented 1 year ago

Have you tried using either Logger.addOutputListener or Logger.addLogListener instead of a separate printer?

Both should provide you with the necessary information, the only difference is that the LogListener is always being called and OutputListener only if the event successfully passed the LogFilter.

Small sidenote: The LogPrinter shouldn't decide whether it logs or not, a LogFilter should be used to determine this.

sarthakydv commented 1 year ago

We were using an older version (1.1.0) of the library #133 fix added in v1.2.0 paved the way for us to write our own LogOutput and handle it.

I am thinking of making a demo project since it comes bundled with the crashlytics dependence.

Please let me know how to integrate these changes in the package/demo project as logging to crashlytics must be something many teams are looking for.

Bungeefan commented 1 year ago

Sorry, but you kinda lost me here. Which version are you using now? 1.1.0 or newer?

If you already use 1.2.0 or higher, you should be able to achieve all this in your custom LogOutput without any workarounds. 1.2.0 and higher already expose the original LogEvent (with all your needed information) via OutputEvent.origin.

sarthakydv commented 1 year ago

Yes yes, that is what we are doing now. I am thinking of adding an example for others to be able to do it like how it was planned in #133.

Just to confirm, the issue is resolved on our end, I am looking to contribute to the project.

Bungeefan commented 1 year ago

Oh, ok, nice, then I am going to go ahead and close this issue as resolved.

Yeah sure, however I thought that the old example in the PR would be enough to give people an idea on how to implement this, but I am certainly not against a real example which showcases the current API!