Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Logging your app's print statements to a local file for later processing.
import 'dart:async';
import 'dart:core';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
void main() => runLoggedApp(App());
// View the log with:
// adb shell run-as <your-android-package> cat /data/data/<your-android-package>/app_flutter/log.txt
void runLoggedApp(Widget app, [String filename = 'log.txt']) async {
final log = File(p.join((await getApplicationDocumentsDirectory()).path, filename)).openWrite(mode: FileMode.append);
runZoned(() {
runApp(app);
}, zoneSpecification: ZoneSpecification(
print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
log.writeln('${DateTime.now().toIso8601String()} | $line');
log.flush();
parent.print(zone, line);
},
));
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('error!');
return Container();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.