Flutter, logging with Firebase crashlytics

Wamae Benson
3 min readOct 29, 2021

So you’ve built your flutter app and now you’ve littered the app with a bunch of print statements. It’s time to get your app to production, you now need to disable the logs; We wouldn’t want anyone to see the logs that could cause a potential breach by exposing some sensitive data like API and encryption keys...

So now you have to go through the code in the app and remove all the print statements. Well, this is reasonable if the code-base is small — What if you have a huge code-base?😫

Logger package to the rescue.😃 — It reminds me of Timber which is an Android dependency.
Logger allows you to print some beautiful logs in Android Studio’s Logcat and have a log filter so that you can have conditions like e.g. Only print logs in the debug variant of the app.

After installing the package, create the following class:

class MyLogFilter extends LogFilter {
@override
bool shouldLog(LogEvent event) {
if (event.level == Level.error) FirebaseCrashlytics.instance.recordError(event.level, event.stackTrace);
return kDebugMode;
}
}

Notice the MyLogFilterclass uses Firebase to log only errors, this will help when you need to track down how many users are facing the error, what caused the error and eventually aid in fixing the error.

Logger myLogger = Logger(
filter: MyLogFilter(),
printer: PrefixPrinter(PrettyPrinter(colors: false)),
);

Create a global variable for the logger in your app, this will make sure you have a single instance of the logger as opposed to multiple instances which could lead to a reduction in app quality and a bad experience for your users.
The logger can also be injected with your favourite dependency injection framework.

Here is how to use the logger in your codebase for the different types of logs.

myLogger.d('Will not be logged in release mode');
myLogger.v('Will not be logged in release mode');
myLogger.i('Will not be logged in release mode');
myLogger.w('Will not be logged in release mode');
myLogger.e('Will be logged in release mode on firebase');
myLogger.wtf('Will not be logged in release mode but probably should');

To put this article into perspective, you have learnt how to log messages, errors, and stitch the crashes to Firebase Crashlytics.

There are other alternatives to Logger like:

NOTE: A bit of unsolicited advice, when choosing a dependency to use in your app you always want to make sure it is up-to-date, used widely…In this case, the Flutter Favourites program would be a good start, it has a curated list of Flutter packages

Well if you’ve made it this far, how about a clap?

References:
Anon., 2022. Crashlytics | FlutterFire. [online] Available at: <https://firebase.flutter.dev/docs/crashlytics/overview/> [Accessed 21 November 2022].

Dart packages. (n.d.). logger | Dart Package. [online] Available at: https://pub.dev/packages/logger [Accessed 16 Nov. 2022].

Wharton, J. (2021). JakeWharton/timber. [online] GitHub. Available at: https://github.com/JakeWharton/timber.

Android Developers. (n.d.). Write and View Logs with Logcat. [online] Available at: https://developer.android.com/studio/debug/am-logcat.

docs.flutter.dev. (n.d.). Flutter Favorite program. [online] Available at: https://docs.flutter.dev/development/packages-and-plugins/favorites [Accessed 16 Nov. 2022].

Dart packages. (n.d.). Search results for logger. [online] Available at: https://pub.dev/packages?q=logger.

‌‌

--

--