Flutter: how to log an Android system message that will get picked up by logcat?

Viewed 235

If I want to log a message that Android Studio will show in its console, I can do this:

import 'dart:developer';

void main() {
  log('Hello world!');
  runApp(const MyApp());
}

But if I package the app, run it and then look for it with logcat, it's not there.

flutter build apk
adb install build/app/outputs/flutter-apk/app-release.apk
adb shell am start -a android.intent.action.MAIN -n com.example.myapp/.MainActivity
adb logcat -t '12-08 00:00:00.000' | grep Hello

This results in empty output. So how do I log a system message, not just an IDE debug message?

1 Answers

Use print() or debugPrint().

You have two options for logging for your application. The first is to use stdout and stderr. Generally, this is done using print() statements, or by importing dart:io and invoking methods on stderr and stdout.

If you output too much at once, then Android sometimes discards some log lines. To avoid this, use debugPrint(), from Flutter’s foundation library.

Check Debugging Flutter apps programmatically for more info.

Related