-
-
Save rodydavis/c8869cf3efb490597efe9e6735e16e93 to your computer and use it in GitHub Desktop.
Logging your app's print statements to a local file for later processing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}, | |
)); | |
log.close(); | |
} | |
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